In addition to the error in the title, it says that A2
and A1
are undeclared identifiers and that Result::calResult:function
does not take 2 arguments, but it does in its implementation. And lastly, it says that left of getValue
must have class/struct/union.
I am new to C++ and am trying to create a program that compares 3 elements of two arrays, I know this is not the best way to do this but I am trying to get my head around objects and classes.
Main.cpp:
#include "stdafx.h"
#include <iostream>
#include "ArrayOfThree.h"
using namespace std;
int main()
{
ArrayOfThree A1, A2;
Result R;
int i = 0;
int input;
for (int i=0;i<2;i++)
{
cin >> input;
A1.set(i, input);
}
for (int i = 0; i < 2; i++)
{
cin >> input;
A2.set(i, input);
}
R.CalResult(A1, A2);
R.outResult();
return 0;
}
ArrayOfThree.h
#include "stdafx.h"
#include <iostream>
#include "Result.h"
using namespace std;
class ArrayOfThree {
private:
int a1, a2, a3;
public:
ArrayOfThree() {
a1 = 0; a2 = 0; a3 = 0;
}
void set(int index,int input) {
if (index == 0)
a1 = input;
else if (index == 1)
a2 = input;
else if (index == 2)
a3 = input;
else
cout << "Index out of bound" << endl;
}
int getValue(int index) {
if (index == 0)
return a1;
else if (index == 1)
return a2;
else if (index == 2)
return a3;
else
cout << "Index out of bound" << endl;
return 0;
}
};
Result.h
#include "stdafx.h"
#include <iostream>
using namespace std;
class Result {
private:
int r1=0, r2=0;
char R;
public:
Result() { r1 = 0; r2 = 0; R = ' '; }
void CalResult(ArrayOfThree A1, ArrayOfThree A2)
{
for (int i = 0; i < 2; i++)
{
if (A1.getValue(i) < A2.getValue(i))
r2++;
else if (A1.getValue(i) > A2.getValue(i))
r1++;
else
r1 = r1;
}
if (r1 < r2)
R = 'B';
else if (r1 > r2)
R = 'A';
else
R = 'T';
}
void outResult()
{
if (R == 'B' || R == 'A')
cout << "The winner is :" << R;
else
cout << "Its a Tie" << endl;
}
};
Looking at your code, your #includes
are a bit messed up.
In your ArrayOfThree.h
header, you're including Result.h
even though you're not using any code from Result.h
in your ArrayOfThree.h
header.
On the other hand, the Result.h
header is using the ArrayOfThree
class which is defined in the ArrayOfThree.h
header, but the Result.h
doesn't include this header.
You have to include the header files of all classes, functions, etc that you're using. So to fix your problem, add #include "ArrayOfThree.h"
into your Result.h
header and remove the #include "Result.h"
from your ArrayOfThree.h
header.
Even better way to structure your code would be to split declaration and definition of your classes. That means you'd have a TU ( Translation Unit ) for your ArrayOfThree
, as well as for Result
classes.
For example, your Result.h
file would become:
#include "stdafx.h"
#include "ArrayOfThree.h"
class Result {
private:
int r1=0, r2=0;
char R;
public:
Result();
void CalResult(ArrayOfThree A1, ArrayOfThree A2);
void outResult();
};
And you'd have a new TU in your project Result.cpp
which would look like this:
#include "Result.h" // In the implementation file, you need to include the
// declaration of the class that you're implementing here.
#include <iostream>
using namespace std;
Result::Result() { r1 = 0; r2 = 0; R = ' '; }
void Result::CalResult(ArrayOfThree A1, ArrayOfThree A2)
{
for (int i = 0; i < 2; i++)
{
if (A1.getValue(i) < A2.getValue(i))
r2++;
else if (A1.getValue(i) > A2.getValue(i))
r1++;
else
r1 = r1;
}
if (r1 < r2)
R = 'B';
else if (r1 > r2)
R = 'A';
else
R = 'T';
}
void Result::outResult()
{
if (R == 'B' || R == 'A')
cout << "The winner is :" << R;
else
cout << "Its a Tie" << endl;
}
Note that now, your Result.h
header doesn't #include <iostream>
. That's because the Result.h
header doesn't depend on anything from iostream
because the parts that were depending on iostream
were moved to the implementation file Result.cpp
, which includes the iostream
header.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.