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;
}

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

Let's see the new code.

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;
}

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.