Thread: expected unqualified ID before'.' token

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    50

    expected unqualified ID before'.' token

    I keep running into this issue, all the information Ive searched for regarding this error have been in regards to nesting classes and other issues. Here's some example code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Chef
    {
    public:
        void makechicken()
        {
            cout<<"The chef makes chicken"<<endl;
        }
        void makeSalad()
        {
            cout<<"The chef makes salad"<<endl;
        }
        void makeSpecialDish()
        {
            cout<<"The chef makes bbq ribs"<<endl;
        }
    };
    
    int main()
    {
        Chef chicken;
        Chef.makeChicken();
    
        return 0;
    }
    Can someone please explain this error and what I'm doing wrong, I went over a few tutorials I watched originally, when I started learning c++, and this usage seems to be correct. Help please.

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    You're invoking the makeChicken() method on the class type Chef. You want to invoke it on the chicken instance.

    P.S. Also, you probably shouldn't name your chef "chicken". Maybe "chef" would be a more suitable name? ;p

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    209
    In case that didn't make sense, what @antred has correctly pointed out is that "chicken" is the variable, "Chef" is a type - a class - used to make "chicken". It can't be used to call methods, only "chicken" (or whatever instance you create) can.

  4. #4
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    Quote Originally Posted by antred View Post
    You're invoking the makeChicken() method on the class type Chef. You want to invoke it on the chicken instance.

    P.S. Also, you probably shouldn't name your chef "chicken". Maybe "chef" would be a more suitable name? ;p
    My mistake, your absolutely right, a lot of the tutorials invoked the function/method on class and instance types that had the same name and that must of got stuck in my brain somehow.

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    Thanks guys! That solves it for the example and two other projects

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,181
    Quote Originally Posted by antred View Post
    P.S. Also, you probably shouldn't name your chef "chicken". Maybe "chef" would be a more suitable name? ;p
    That might be an idea for a sequel to Ratatouille
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected unqualified-id before '{' token...
    By NSKBpro in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2012, 04:17 PM
  2. expected ) before ; token
    By dwolf559 in forum C Programming
    Replies: 2
    Last Post: 10-03-2012, 11:06 PM
  3. error: expected unqualified-id before '{' token
    By fhbwghads in forum C++ Programming
    Replies: 5
    Last Post: 12-25-2008, 04:39 AM
  4. error: expected unqualified-id before '=' token
    By (::) in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2008, 11:50 PM
  5. Error: expected unqualified-id before 'using'
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2006, 05:24 AM

Tags for this Thread