i am using Visual Studio 2010 Ultimate and i want to create a class to read .ini files in C++. It is not hard for me but Visual underline method when it return reference to an object and the error message is not clear for me so I need some help.
I have two class. One, Tokenizer, which read .ini files and the second, Token, which represent one token like [Somethink].
In Tokenizer i created a method to add new Tokens to the std::list when there are found in the ini file.
Token Tokenizer::addToken(string name){
Token newToken(name);
data.push_back(newToken);
return newToken;
}
I need that reference to add pairs key->value to it later. This is the contructor of Token:
Token::Token(string tokenName){
name = tokenName;
}
And everything should by ok but it’s not. Visual Studio underline the word addToken in the code above saying that:
Error: declaration is incompatible with “error-type>
Tokenizer::addToken(std::string name)”.
But the declaration is
Token Tokenizer::addToken(string name);
and class Token is also defined so it makes no sense to me. What is interesting when i change the type of returned value to VOID and remove the return instruction then everything is ok.
Please help. This is my first contact with references and Object programming in C++.
This would be easier if you showed more code. Using my psychic debugging abilities, I’m going to guess that
Tokenis a nested class/struct. I.e., you need to define it likeI’ve guessed the namespace qualification to be Tokenizer::Token (but it might need to be something else)
Note also that it is not necessary to fully qualify the nested types (if they would be locally visible from withing
Tokenizer) forJust the return type needs full qualification, IIRC