//Header
struct cheese
{
cheese(int number1 = 5, int number2 = 5);
int getNumber1(void);
int getNumber2(void);
int multiply(void);
void setNumber1(int);
void setNumber2(int);
private:
int x;
int y;
}win;
//.cpp
#include "cheese.h"
cheese::cheese(int number1, int number2)
{
setNumber1(number1);
setNumber2(number2);
}
int cheese::getNumber1(void)
{
return x;
}
int cheese::getNumber2(void)
{
return y;
}
int cheese::multiply(void)
{
return getNumber1() * getNumber2();
}
void cheese::setNumber1(int number)
{
x = number;
}
void cheese::setNumber2(int number)
{
y = number;
}
//main()
#include "cheese.h"
#include <iostream>
usingnamespace std;
int main()
{
cout << "Multiplication: " << win.multiply() << endl;
}
I am trying to use the "win" object from the "cheese" class to access the member function "multiply()." The only error message I am getting is "struct cheese win already defined in cheese object." I don't know how to fix this and I would like to know how I would use the "win" object of "cheese" class to call member functions. I am aware that I could create an object of the class by doing:
The problem is since you are creating a cheese object called 'win' inside the header, both your main file and cheese.cpp are trying to create a 'win' object separate and so you have your error.
You can only declare the object in one source file; if other files need to use the same one they need to declare it as extern.