-
April 30th, 2012, 09:13 AM
#1
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.
-
April 30th, 2012, 09:33 AM
#2
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>
-
April 30th, 2012, 09:41 AM
#3
Re: strange error returning template class from member function
btw, my IDE is visual c++ 2010 express
-
April 30th, 2012, 09:42 AM
#4
Re: strange error returning template class from member function
I typed it in. Thanks for the tips.
-
April 30th, 2012, 10:06 AM
#5
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?
Last edited by demma; April 30th, 2012 at 10:09 AM.
-
April 30th, 2012, 10:25 AM
#6
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.
-
April 30th, 2012, 10:35 AM
#7
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.
-
April 30th, 2012, 10:40 AM
#8
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...
-
May 1st, 2012, 04:36 PM
#9
Re: strange error returning template class from member function
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|