strange error returning template class from member function
When I do this:
// header file:
#include <list>
using namespace std;
class myClass {
list<int> myMethod();
};
// cpp file:
list<int> myClass::myMethod() {
}
In the cpp file, 'myMethod' is underlined in red and when I hover over it, it says:
"std::list<int, std::allocator<int>> myClass::myMethod()
Error: declaration is incompatible with "<error-type> myClass::myMethod()""
But when I make it as a standalone function, outside a class, with no pre-declaration, there is no problem.
What's going on?
Thanks in advance.
Re: strange error returning template class from member function
That should work. Have you typed that or did you cut & paste from your actual code?
When posting code please embed it in code tags to preserve the indentation [code]Paste code here[/code]
Oh, one more thing. You should never ever add using namespace to a header. In this case you should instead use std::list<int>
Re: strange error returning template class from member function
btw, my IDE is visual c++ 2010 express
Re: strange error returning template class from member function
I typed it in. Thanks for the tips.
Re: strange error returning template class from member function
hmmm, it works now that I have explicitly included <list> instead of just these:
#include <iostream>
#include <fstream>
#include <string>
#include <boost\regex.hpp>
#include <math.h>
#include <time.h>
I don't know which one it was in, but it must have been in one of the top 3, because when I tried to use a method of a list, my IDE showed a list of the methods of std::list. So including <list> shouldn't have made any difference. Any one understand why it did, or might have?
Re: strange error returning template class from member function
Yes that might be a bit confusing. When you hover over your code it's the intellisense that provides you with all details, not the compiler/linker. Sometimes it's dead stupid and finds nothing but sometimes it's very smart like this time. I.e. even though you hadn't included list the intellisense guessed it was std::list you meant so it provided that info for you.
Re: strange error returning template class from member function
In that case it should have given me a far simpler error message: it should have simply underlined like so:
list<int> myMethod();
In the declaration in the class. That's what it would normally do if you tried to use an undefined type.
Re: strange error returning template class from member function
Hard to tell what did go wrong without having the real code. Usually I find MSVC (and also other compilers) to provide quite resonable errors these days. You should have seen them +10 years ago... :cry:
Re: strange error returning template class from member function
Quote:
Originally Posted by
demma
In that case it should have given me a far simpler error message: it should have simply underlined like so:
list<int> myMethod();
In the declaration in the class. That's what it would normally do if you tried to use an undefined type.
Again, this feature is not the compiler/linker. So, as SMA said, it may be "smarter" then you, thinking that you are going to include the proper standard header file.
This is also one of the reasons I don't use Intellisense. It just annoys the crap out of me!
Viggy