Can anyone spot a problem with this code?

#include<iostream>
using namespace std;

class DumbBell
{
int weight;
public:
int setWeight(int);
};
int DumbBell :: setWeight(int w)
{
int(weight) = w;
}

int main()
{
DumbBell bar;
bar.setWeight (200);
cout << "The weight is " << bar.setWeight() << endl;
system("pause");
return 0;
}
Do you have a compiler? Just try to compile it.
A few things. First, in your definition of int DumbBell::setWeight(int) you are casting the member variable to an int and setting it equal to w. Member variable weight is already declared as an int in the class DumbBell, so you shouldn't cast it. Second, this member function is expected to return an int. You probably should make it void. Third, you are trying to cout bar.setWeight(). setWeight(int) needs to take an int argument. You can't call it with a void as you are here. You should write an accessor member function called something like int getWeight() for your class and just have it return weight. One last thing, you might also want to write a ctor for the class that initializes weight to 0 or something so if someone tries to call getWeight() before they use setWeight(int), it won't return some random garbage value.

~psault
Am still geting an error on the cout line, an expression expected before int



#include<iostream>
using namespace std;

class DumbBell
{
int weight;
public:
void setWeight(int);
int getWeight(int) {return (weight);}
};

void DumbBell :: setWeight(int w)
{
weight = w;
}

int main()
{
DumbBell bar;
bar.setWeight (200);

cout << "The weight is " << bar.getWeight(int) << endl;

system("pause");
return 0;
}//invoice.cpp
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
#include<iostream>
using namespace std;

class DumbBell
{
    int weight;
public:
    void setWeight(int);
    int getWeight(int) {return (weight);} //Why does this function take a parameter - it is not used
};

void DumbBell :: setWeight(int w)
{
    weight = w;
}

int main()
{
    DumbBell bar;
    bar.setWeight (200);

    cout << "The weight is " << bar.getWeight(int) << endl; // bar.getWeight(int)  -  this is not the correct way to call a function anyway

    system("pause");
    return 0;
}//invoice.cpp 


So whats your opinion guestgulkan .

Tried alternatives but did do so well too
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>
using namespace std;

class DumbBell
{
    int weight;
public:
    void setWeight(int);
    int getWeight() {return (weight);} 
};

void DumbBell :: setWeight(int w)
{
    weight = w;
}

int main()
{
    DumbBell bar;
    bar.setWeight (200);

    cout << "The weight is " << bar.getWeight() << endl; 

    system("pause");
    return 0;
}
Thanks guestgulkan , classes and functions are still bullying me: Here is another one, am tracing the error but cant really tell what is going on!!

//#include<student.h>
#include<iostream>
using namespace std;

class student
{
public:
int studentId;
string studentName;
int CourseNo;
bool enrolmentstatus;
//void getDetails(int,string,int,bool);
void display(int,string,int,bool);
};

void student :: display(int,string,int,bool)
{
cout << studentId
<< studentName
<< CourseNo
<< enrolmentstatus;
}

int main()
{
student firstyear (
16886272,
"George William",
139303,
true);
//firstyear.display ;
cout << "Your student details are: " <<
firstyear.display() << endl;

system("pause");
return 0;
}
when you try to initialize a class, you always call the constructor as default, and the paremeters you are trying to pass is to a constructor that doesn't exist ;)

so it should be something like this:

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

class student
{
public:
int studentId;
string studentName;
int CourseNo;
bool enrolmentstatus;
//void getDetails(int,string,int,bool);
void display();
//here is a constructor that takes the paremeters and sets the variables to what they should be
student(int iStudentId, string sStudentName, int iCourseNo, bool bEnrolmentStatus)
{
studentId = iStudentId;
studentName = sStudentName;
CourseNo = iCourseNo;
enrolmentstatus = bEnrolmentStatus;
}
};

void student :: display(int,string,int,bool)
{
cout << studentId
<< studentName
<< CourseNo
<< enrolmentstatus;
}

int main()
{
student firstyear (
16886272,
"George William",
139303,
true);
//firstyear.display ;
cout << "Your student details are: " <<
firstyear.display() << endl;

system("pause");
return 0;
}
prototype for `void student::display(int, std::string, int, bool)' does not match any in class `student'

This error is coming up, though at look, it looks like the protype is maching well with the function call
code modified but still cant make it:
ERROR: No matching function for call to `student::student()'

//#include<student.h>
#include<iostream>
using namespace std;

class student
{
private:
int studentId;
string studentName;
int courseNo;
bool enrolmentstatus;

public :
student (int , string, int , bool);
void display(int,string,int, bool);
};

student :: student (int a, string b,int c, bool d)
{
studentId = a;
studentName = b;
courseNo = c ;
enrolmentstatus = d ;

}

int main()
{
student firstyear ;

int studentId =16886272;
string studentName = "George William";
int courseNo =139303;
bool enrolmentstatus = true ;

cout << "your student details are: " << firstyear.display (a,b,c,d ) << endl;

system("pause");
return 0;
}
what line is the error?

as far as i can see from the error, it is trying to call the constructor that does not have any paremeters, however your constructor does. you could use constructor overloading. having multiple constructors with different paremeters.
Topic archived. No new replies allowed.