1
votes

I'm trying to declare a static vector of my own class type (SY::Symbol) like this:

SymbolsTable.h

namespace SYT
{
    class SymbolsTable
    {
        public:
            static std::vector<SY::Symbol> m_symbols;
                    void addToken(TK::Token);
    };
}

and I want to use it inside the method addToken in my SymbolsTable.cpp file.

SymbolsTable.cpp

#include "../../includes/px/SymbolsTable.h"
#include "../../includes/px/Token.h"

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>

std::vector<SY::Symbol> SYT::SymbolsTable::m_symbols(std::initializer_list<SY::Symbol>);

SYT::SymbolsTable::SymbolsTable()
{

}

void SYT::SymbolsTable::addToken(TK::Token token)
{
    int index = getSymbolIndex(token); 

    if(index == -1)
    {
        SY::Symbol symb(token, "");
        SYT::SymbolsTable::m_symbols.push_back(symb);
        token.setIndex(indexOf(m_symbols, symb)); 
    }
    else
    {
        token.setIndex(index);
    }       
}

I've tried declaring and initializing it outside the class inside my SymbolsTable.cpp, but I guet the error.

error: no 'std::vector SYT::SymbolsTable::m_symbols(std::initializer_list)' member function declared in class 'SYT::SymbolsTable'

As you can see, I have to initialize it, so I'm trying to use this initializer_list, which I don't know if I'm doing it correct.

So, I know that I have to initialize the vector before everything.

My question is:

  • where to initialize it;
  • how to inialize it.
2

2 Answers

2
votes
std::initializer_list<SY::Symbol>

This is a type so the compiler is reading this as a function definition which has not been declared in the specified scope, thus the error.

You can write

std::vector<SY::Symbol> SYT::SymbolsTable::m_symbols;

To just define it, or

std::vector<SY::Symbol> SYT::SymbolsTable::m_symbols{1,2,3,4};

to initialize it - note replace 1-4 with valid SY::Symbol objects.

1
votes

you can just write

std::vector<SY::Symbol> SYT::SymbolsTable::m_symbols;

in the cpp file.

this uses the default constructor, initializing the vector to an empty vector.

however, most likely you don't really want a static member, because it feels very wrong to have non-static member functions update a static data member. rather you probably want a static instance of the class. and even that is probably a sub-optimal design, but still probably an improvement.