Strange compiler error

This is a discussion on Strange compiler error within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> class foo { public: foo() { std::cout << "foo"; } void bar() { std::cout << "bar"; } ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Strange compiler error

    Code:
    #include <iostream>
    
    class foo {
    public:
        foo() { std::cout << "foo"; }
        void bar() { std::cout << "bar"; }
    };
    
    int main() {
        foo f();
        f.bar();
    };
    For some reason I get the error "left of '.bar' must have class/struct/union". Does anyone know why?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Does changing the line above to foo f; help?

    (Edit: Perhaps g++'s error message will give you more of my point:
    Code:
    temp.cpp.11: error: request for member `bar' in `f', which is of non-class type `foo ()()'
    That is to say, foo f(); does not declare an instance of class foo, but is a prototype for a function.)
    Last edited by tabstop; 10-21-2008 at 09:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 03:39 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21