undefined string.

i declared a random int named position in int main() and when i was making a class it said that it was undefined. help please???????????????
How do you expect us to tell you what the problem is? We can't see your code. Post your code if you want help.
im making a motocross racing game.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;
using std::cin;

int main()
{
    srand(static_cast<unsigned int>(time(0)));
    string firstName, lastName, age, phone, bikeSize, brand;
    int position = rand(), gameCompletions = 0;
    char choice, tutorial_continue;
    system ("TITLE mx career!!");
    cout << "\t\t\t\t mx career";
    cout << "\n\n\nwelcome to your motocross career. here you will make choices depending";
    cout << "\non what you think is best. but before you begin we will need you to fill";
    cout << "\nout this survey..\n\n";
    
    /*survey*/
    cout << "\t\t\t\tsurvey";
    cout << "\n\nFirst name:\n";
    cin >> firstName;
    cout << "last name:\n";
    cin >> lastName;
    cout << "age:\n";
    cin >> age;
    cout << "phone #\n";
    cin >> phone;
    cout << "bike size. (enter 'a' for 250 or 'b' for 450):\n";
    cin >> choice;
    switch (choice)
           {
                   case'a':
                        bikeSize = 250;
                        break;
                   case'b':
                        bikeSize = 450;
           }
    cout << "bike brand(a = kawasaki, b = ktm, c = honda, d = suzuki, e = gasgas, f = yamaha)";
    cin >> choice;
    switch (choice)
    {
           case'a':
           system("COLOR A");
           brand == "kawasaki";
           break;
           case'b':
           system("COLOR C");
           brand == "ktm";
           break;
           case'c':
           system("COLOR 4");
           brand == "honda";
           break;
           case'd':
           system("COLOR E");
           brand == "suzuki";
           break;
           case'e':
           system("COLOR F4");
           brand == "gasgas";
           break;
           case'f':
           system("COLOR 9");
           brand == "yamaha";
           break;
    }
    cout << "\nonce you have completed the game you will be able to create your own race team\n";
    cout << "note:once you quit the game your data will not be saved\n";
    cout << "\nso what do you want to do first??? \n(a = play game or b = cheat)";
    cin >> choice;
    switch (choice)
    {
           case'a':
                   break;
           case'b':
                   string cheat;
                   cin >> cheat;
                   if (cheat == "speedy gonzales")
                   {
                             cout << "you are a big fat cheater, but i guess i can make an exception if you do it for speed\n";
                   }
                   else if (cheat == "make it rain")
                   {
                        cout << "anything for money...";
                   }
                   
}
/**********************************************CLASSES***********************************/


class bike
{
      public:
             int speed;
             int breaking;
             void broken()
             {
                  position = position - 18;
                  cout << "OH NO!!!! YOUR BIKE BROKE!!!!!!!!!!";
             }
};
 }


ps. the class isnt finished.
The class is undefined because you didn't declare it. You're defining the class AFTER instantiating it. Does that make sense to you? Either put it above the main function, or put it in a separate file. As far as the undefined INT goes, it's undefined because it's out of scope. You can't declare a variable in one function and use it in a scope where it isn't defined yet. If you want to use position, declare it in the class or pass it to the function.
Last edited on
class shall be declared in header file and then included with #include "headerName"
implemetation can be then in seperate cpp or in this cpp.

or just put class bike; on top of this cpp befor main ();
Last edited on
thanx yall :) but when packetpirate said
pass it to the function
what do you mean?
Last edited on
Pass it as a parameter to the function.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;

class test {
    public:
        void setString(string y)
        {
            x = y;
        }
        void print()
        {
            cout << x << endl;
        }
    private:
        string x;
};

int main(int argc, char** argv)
{
    test object;
    object.setString("testing 1,2,3");
    object.print();

    return 0;
}
Last edited on
i dont really getb it
more importantly read this:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

also why not just write
 
using namespace std;


instead of writing out each specific method, just seems unncessary
Last edited on
less includes and using --> faster build :D
Last edited on
How the hell do you learn about classes before function parameters?

Also, ascii, because using the entire standard namespace wastes resources. In a small program like this, it isn't that important, but I've made a habit of doing it because it's bad practice to include an entire namespace.
Last edited on
Topic archived. No new replies allowed.