I'm getting these types of errors: identifier "gm" is undefined or expected a ';'
How do I fix these errors?

#include <iostream>
#include <cstdlib> //srand
#include <ctime> //time
#include <cmath>
#include <string>

using namespace std;

int G_master;
int m_secret;
int m_choice;
int m_win, m_prev_delta;
int Number_guess;
int Card_guess;
int delta;
int endchk;  


class G_master

public:
    int m_secret;
    int m_guess;
    int m_win;
    int m_prev_delta;

    void setup();
    void hello() { cout << "Hello\n"; }
    void listen();
    void respond();
    void menu();
    int endchk();
    void prompt();
    void cleanup();
    void conversation();
};

int main()
{
    G_master gm;
    gm.conversation();
    return 0;
}

void G_master::conversation()
{
    setup();
    hello();
    do{
        listen();
        respond();
    } while (endchk());
    cleanup();
}

void G_master::respond()
{
    switch (m_choice)
    {
    case 1:
        {  Number_guess ng; 
           ng.conversation();
           break;
        }
    case 2:
        {  Card_guess cg;
           cg.conversation();
           break;
        }
    default: 
    }
}

void G_master::menu()
{
    string menu = 0;
    cout << menu;
}

void G_master::listen()
{
    menu();
    cin >> m_choice;
}

Dani AI

Generated

Quick diagnosis: those Visual Studio errors (missing ; before gm, undeclared gm/cg) come from name collisions and missing type definitions. In this thread was right to flag the broken class syntax, correctly called out the top-level int G_master conflict, and spotted that Number_guess/Card_guess are currently treated as integers rather than types. ’s suggestion about ordering/prototypes is also relevant: the compiler must know a type before you instantiate it.

Concrete fixes (in order of priority)

  1. Remove the top-level declarations that reuse the same identifiers as your classes or members (e.g. the globals named the same as G_master, Number_guess, Card_guess, and any m_* globals). Those hide the class names and confuse the parser.
  2. Put instance/state variables (choice, secret, win, prev_delta, etc.) inside G_master as members instead of globals. That makes respond() refer to this->m_choice rather than an unrelated global.
  3. Make sure class G_master has a proper opening { and a closing }; (the trailing semicolon after a class is required). That fixes a lot of syntax noise.
  4. Define Number_guess and Card_guess as actual classes/structs before you try to do Number_guess ng; or Card_guess cg;. If you must keep their definitions later, you can use pointers and forward declarations, but the easiest approach is to move those class definitions above G_master::respond.
  5. Fix small semantic issues (don’t initialize a std::string with 0; use "" or a literal; don’t have a global named endchk if endchk() is a member function).

Minimal ordering pattern (illustrative)

class Number_guess { public: void conversation(); };
class Card_guess   { public: void conversation(); };

class G_master {
    int m_choice;
public:
    void conversation();
    void respond();
};

int main() { G_master gm; gm.conversation(); }

Final notes: compile after each change — the first compiler error is usually the root cause and later messages cascade. Prefer private members + public methods for encapsulation (as suggested). Correcting the name collisions and the class/type order will resolve the C2065/C2146/C2228 errors you posted.

Recommended Answers

All 23 Replies

Wait so where do I put main at the very end?

Wait? For what? Since you are not using prototypes, you move the main and it's content to the last line. Pretty simple fix.

I did that but it didn't work.

You are missing a right-curly-brace { before the start of your G_master class. Try this instead:

class G_master {
public:
    int m_secret;
    int m_guess;
    int m_win;
    int m_prev_delta;
    void setup();
    void hello() { cout << "Hello\n"; }
    void listen();
    void respond();
    void menu();
    int endchk();
    void prompt();
    void cleanup();
    void conversation();
};

Also, unless there is a REALLY good reason, all member variables like m_secret, etc. should be private with public or protected getter/setter methods as needed.

That looks the same except my curly brace was on the bottom.?

I tired that but I'm still getting the same errors

I inserted the one he showed but it was same

#include <iostream>
#include <cstdlib> //srand
#include <ctime> //time
#include <cmath>
#include <string>

using namespace std;

int G_master;
int m_secret;
int m_choice;
int m_win, m_prev_delta;
int Number_guess;
int Card_guess;
int delta;
int endchk;  


class G_master {
public:
    int m_secret;
    int m_guess;
    int m_win;
    int m_prev_delta;
    void setup();
    void hello() { cout << "Hello\n"; }
    void listen();
    void respond();
    void menu();
    int endchk();
    void prompt();
    void cleanup();
    void conversation();
};



void G_master::conversation()
{
    setup();
    hello();
    do{
        listen();
        respond();
    } while (endchk());
    cleanup();
}

void G_master::respond()
{
    switch (m_choice)
    {
    case 1:
        {  Number_guess ng; 
           ng.conversation();
           break;
        }
    case 2:
        {  Card_guess cg;
           cg.conversation();
           break;
        }
    default: break;
    }
}

void G_master::menu()
{
    string menu = 0;
    cout << menu;
}

void G_master::listen()
{
    menu();
    cin >> m_choice;
}

int main()
{
    G_master gm;
    gm.conversation();
    return 0;
}

OK, that fixed one thing but read for an example of using a class. Your main didn't make an object to work on.

Also, you could pull your class functions into the class.

i see you got an extra semicolon after the curly brace at the end of your G_master class. Could be the thing causing the problem, all else looks good

It still not working for me.

Which compiler and IDE are you using?

"It still not working" is too vague. An error message would be slightly better.

These are the type of errors:

Error   6   error C2065: 'cg' : undeclared identifier
Error   10  error C2065: 'gm' : undeclared identifier   
Error   9   error C2146: syntax error : missing ';' before identifier 'gm'
Error   4   error C2228: left of '.conversation' must have class/struct/union   

I'm using microsoft visual studio 2013

Referring to your last post with code. Why are yoy defining an int as G_master (line 9) and a class as G_master (line 19)?

Even when I deleted int G_master, it didn't change anything.

it didn't change anything.

Could you tell us WHAT happened EXACTLY, please. We have absolutely no need of someone yelling it "doesn't work", "it fails", "I get errors" etc.
A complete list of errors with the line numbers where the errors happened, can do miracles sometimes.

From what I see at a glance, you've declared a class G_master
Above that you have variables with the same names as variables and functions in your class.
Many of the class functions are undefined and some make no sense.
e.g

    void G_master::respond()
    {
        switch (m_choice)
        {
        case 1:
            {  Number_guess ng; 
               ng.conversation();
               break;
            }
        case 2:
            {  Card_guess cg;
               cg.conversation();
               break;
            }
        default: break;
        }
    }

Where is m_choice declared and how is m_choice passed to ::respond()?
Then you have Number_guess ng; - Number_guess is declared as an integer, so how would this integer have a conversation? - ng.conversation();
Ditto for Card_guess cg;

Are you wanting to have a class G_master and then derive other classes from it?
If you can explain exactly what the end goal is, someone may be able to point you in the right direction. :)

I'm not even sure, this is kind of how my professor did it.

We're not interested how your professor did it. In order to help you we like to know how you did it. Still waiting for information on behaviour of your program. DON'T tell us it didn't work. You and we know that. Errorcodes, line numbers where errors happen, what happens when you run it etc. etc.

You seem lost with repect to coding for a class (or a struct?) in C++

You best start simple ...

Here is a simple example of using a C++ struct with a constructor ...

Note:
in C++ the contents of a struct have default state of 'public'
in C++ the contents of a class have default state of 'private'
OTHERWISE ... a class and a struct are the same.
You can change the (default) status by begining a block of code with a label like this:

struct Student
{
private:
    std::string name;
    int id;
 public:
     // ctor ...
     Student( const std::string& name="", id=0 ) : name(name), id(id) {}

     void.print() const
     {
         std::cout << name << ' ' << id;
     }
} ;

Here is a simple guessing game example that could give you ideas to help ease your coding ...

// class_Guess.cpp //


#include <iostream>
#include <sstream> // re. stringstream
#include <string>
#include <ctime> // re. time
#include <cstdlib> // re. srand, rand


using namespace std;

const int MAX_NUM = 100;


// some nice little utitities to ease student coding ... //
int takeInInt( const string& msg, int min, int max )
{
    int val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
        {
            if( min <= val && val <= max )
                break;
            cout << "Valid input range here is "
                 << min << ".." << max << " ...\n";
        }
        else
        {
            cout << "Invalid input ... integers only!\n";
            cin.clear(); // clear error flasgs
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}
int takeInChr( const string& msg = "" )
{
    cout << msg << flush;
    string reply;
    getline( cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}

bool more()
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' )
        return false;
    // else ...
    return true;
}



struct GuessMaster
{
    int secret_num;
    int guess_num;
    int delta;

    // default ctor...
    GuessMaster() : secret_num( rand() % MAX_NUM + 1 ), guess_num(0), delta(-999) {}
} ;



int main()
{
    srand( time(0) );

    do
    {
        GuessMaster gm;
        int count = 0;
        while( gm.guess_num != gm.secret_num )
        {
            // form prompt ...
            stringstream ss;
            ss << "Your guess in range 1 to " << MAX_NUM << ": ";
            gm.guess_num = takeInInt( ss.str(), 1, MAX_NUM );
            gm.delta = gm.guess_num - gm.secret_num;
            ++count;
            cout << "For guess " << count << ", you ";
            if( gm.delta < 0 )
                cout << "were low by " << -gm.delta << '\n';
            else if( gm.delta > 0 )
                cout << "were high by " << gm.delta << '\n';
            else
                cout << "guessed it!\n";
        }
    }
    while( more() );
}

Oops ... missed fixing a typo within the 30 minute allotted edit time...
There was an erroneous period (.) between 'void' and 'print() const'
Now fixed in modified class Student example below:

class Student
{
 public:
     // ctor ...
     Student( const std::string& name="", id=0 ) : name(name), id(id) {}

     void print() const
     {
         std::cout << name << ' ' << id;
     }
private:
    std::string name;
    int id;
} ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.