Program Help

Pages: 12
Ok so i want to make a program using vectors and to simplify things im going to use a class, there are a few things i want to know:

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class vars
{
    public:
    void game();
    vars()
    {
        money = 0;
        xp = 0;
        Pname = "";
        Bname = "";
    }
    long long int getMoney(){return money;}
    int getXp(){return xp;}
    string getPName(){return Pname;}
    string getBName(){return Bname;}

    private:
        long long int money;
        int xp;
        vector<string> Products;
        vector<int> Prices;
        string Pname;
        string Bname;
};

int main()
{
    int choice;
    vars vo;

    cout << "1) Load" << endl;
    cout << "2) New" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            cout << "Welcome, in this game you make your own shop from the ground up and" << endl;
            cout << "build a mega store\n" << endl;

            cout << "Lets start by getting some information" << endl;
            break;
        case 2:
            vo.game();
            break;
    }
}

void game()
{

}


#1. I keep getting an error:

obj\Debug\main.o||In function `main':|
C:\Users\Chay Hawk\Desktop\Shop Owner\main.cpp|50|undefined reference to `vars::game()'|
||=== Build finished: 1 errors, 0 warnings ===|

I dont get why i keep getting that.

#2. how do i get vectors from a class? the same way as the other variables? does it need to be returned?

#3. Am i making my class right?
Last edited on
1) You're not defining your game() function.

2)What do you mean by "get" ? You use a vector the same way you use it normally. But in your class, the vector is private, so you probably would make functions to add elements to your vectors.

3)There is no right way to make a class, it depends on your preferences. But a class to hold variables, isn't a very good idea.
1. Line 55 should be:
void vars::game()
2. You can return a pointer
3. form what I can see yes
1) Basically what Dash said; you haven't defined your game() function.

void vars::game() { }

2) You should create a getter(inspector) method inside your class to get the values held in the vector. Either that or you could have a public iterator.

1
2
3
4
5
6
7
8
vars() : iter(Prices.begin())
    {
        money = 0;
        xp = 0;
        Pname = "";
        Bname = "";
    }
    vector<int>::const_iterator iter;

3) Classes weren't really made to just hold variables.
Last edited on
Ok i fixed the void vars::game() {} one. It seems to me that no matter what i do i just cant undertand classes. :( ive watched so many tutorials and i did read some articles and i ust dont get it.
So should i just use a struct then? wait a minute... i think something just came to me. structs are for holding variables and classes are for grouping similar functions together, is that right?
The only difference between a struct and a class is the default access level.
Here's an example of the use of classes. You should only use a class if you are going to have more than one object. That's how I think of it. My only exception is if the class is a wrapper for something else.

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
#include <iostream>
#include <vector>
#include <string>

class Student {
    private:
        std::string name;
        int age;
    public:
        Student(const int a, const std::string n) : age(a), name(n) { }
        std::string getName(void) const { return name; }
        int         getAge (void) const { return age;  }
}; 

int main(void) {
    std::vector<Student> studentCont;
    std::vector<Student>::const_iterator iter;
    Student a(14, "Nancy");
    Student b(12, "James"); 
    Student c(12, "Jessie");
    studentCont.push_back(a);
    studentCont.push_back(b);
    studentCont.push_back(c);
    for(iter = studentCont.begin(); iter != studentCont.end(); ++iter)
        std::cout << iter->getName() << " is " << iter->getAge() << " years old.\n";
    return 0;
}
Last edited on
I understand all of that except for these lines

 
friend std::ostream& operator << (std::ostream&, const Student&);


1
2
3
std::ostream& operator << (std::ostream &os, const Student &s1) {
    return os << s1.name << " is " << s1.age << " years old.\n";
}
That is operator overloading. The documentation would probably explain that better than I'd do: http://www.cplusplus.com/doc/tutorial/classes2/ .
If you want to pass around all those vars to a different function (which needs a lot of them, not just one, otherwise there is little point) I'd make it struct (or class if you want, just preference really)

and don't forget to pass the struct/class by (const if you don't plan to change a var) reference.
I revised it so I wouldn't have to explain that, but I'll do it anyway. :)

1
2
3
std::ostream& operator << (std::ostream &os, const Student &s1) {
    return os << s1.name << " is " << s1.age << " years old.\n";
}


That's overloading the insertion operator. The overloaded operator '<<' takes an object of the std::ostream class and an object of the Student class. It takes 'cout' and the Student object that 'iter' is currently on, then returns the object's information to the 'cout' stream object.

Was that a good explanation? I hardly ever work with classes so I'm not even sure if that is correct. If it isn't and anybody notices then please point it out.
I still dont understand the

std::ostream& operator << (std::ostream &os, const Student &s1) {
return os << s1.name << " is " << s1.age << " years old.\n";
}

part but i understand the changed code now.

Also

"There is no right way to make a class, it depends on your preferences. But a class to hold variables, isn't a very good idea."

That isnt what your example class is doing is it?
Last edited on
That isnt what your example class is doing is it?

I'm sorry, but may I ask where is my "example class" ?
Ok and in addition to my last question i got an error and i bet its something stupid again.

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class vars
{
    public:
    void game();
    vars(long long int M, int XP, string PN, string BN):
         money(M), xp(XP), Pname(PN), Bname(BN){}
    long long int getMoney(){return money;}
    int getXP(){return xp;}
    string getPname(){return Pname;}
    string getBname(){return Bname;}

    private:
        long long int money;
        int xp;
        string Pname;
        string Bname;
};

int main()
{
    int choice;

    vars vo;

    cout << "1) Load" << endl;
    cout << "2) New" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            cout << "Welcome, in this game you make your own shop from the ground up and" << endl;
            cout << "build a mega store\n" << endl;

            cout << "Lets start by getting some information" << endl;
            break;
        case 2:
            vo.game();
            break;
    }
}

void vars::game()
{
    cout << money << endl;
}



C:\Users\Chay Hawk\Desktop\Shop Owner\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Shop Owner\main.cpp|29|error: no matching function for call to 'vars::vars()'|
C:\Users\Chay Hawk\Desktop\Shop Owner\main.cpp|11|note: candidates are: vars::vars(long long int, int, std::string, std::string)|
C:\Users\Chay Hawk\Desktop\Shop Owner\main.cpp|8|note: vars::vars(const vars&)|
||=== Build finished: 1 errors, 0 warnings ===|
vars vo;

There is no default constructor for your class.

something like this example?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// example: class constructor
#include <iostream>
using namespace std;

class CRectangle {
    int width, height;
  public:
    CRectangle (int,int);
    int area () {return (width*height);}
};

CRectangle::CRectangle (int a, int b) {
  width = a;
  height = b;
}

int main () {
  CRectangle rect (3,4);
  CRectangle rectb (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;
  return 0;
}



is this a constructor:

1
2
3
CRectangle::CRectangle (int a, int b) {
  width = a;
  height = b;
#include<iostream>
#include <string>
#include <vector>

using namespace std;

class vars
{
public:
void game();
vars(long long int M, int XP, string PN, string BN);
vars(){}
long long int getMoney(){return money;}
int getXP(){return xp;}
string getPname(){return Pname;}
string getBname(){return Bname;}

private:
long long int money;
int xp;
string Pname;
string Bname;
};

vars::vars(long long int M, int XP, string PN, string BN): money(M), xp(XP), Pname(PN), Bname(BN)
{
}

void vars::game()
{
cout << money << endl;
}


int main()
{
int choice;

vars *vo=new vars();

cout << "1) Load" << endl;
cout << "2) New" << endl;
cin >> choice;

switch(choice)
{
case 1:
cout << "Welcome, in this game you make your own shop from the ground up and" << endl;
cout << "build a mega store\n" << endl;

cout << "Lets start by getting some information" << endl;
break;
case 2:
vo->game();
break;
}


system("pause");

return 0;
}


How this help just help u debug it
Ok i got it working by putting vars(){} in there and deleting the : and the stuff after vars(long long int M, int XP, string PN, string BN), why do i need an empty constructor? at what line should i make a destructor for the class?
Last edited on
Pages: 12