CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2012
    Posts
    5

    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.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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>
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Apr 2012
    Posts
    5

    Re: strange error returning template class from member function

    btw, my IDE is visual c++ 2010 express

  4. #4
    Join Date
    Apr 2012
    Posts
    5

    Re: strange error returning template class from member function

    I typed it in. Thanks for the tips.

  5. #5
    Join Date
    Apr 2012
    Posts
    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.

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  7. #7
    Join Date
    Apr 2012
    Posts
    5

    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.

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  9. #9
    Join Date
    Feb 2002
    Posts
    4,640

    Re: strange error returning template class from member function

    Quote Originally Posted by demma View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured