错误:"Left of getValue must have class/struct/union"

Error: "Left of getValue must have class/struct/union"

本文关键字:class union struct have getValue Left of 错误 must      更新时间:2023-10-16

除了标题中的错误外,它还说A2A1是未声明的标识符,并且Result::calResult:function不接受 2 个参数,但它在其实现中确实如此。最后,它说getValue左派必须有阶级/结构/联合。

我是C++新手,正在尝试创建一个比较两个数组的 3 个元素的程序,我知道这不是最好的方法,但我正在尝试了解对象和类。

主.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;
}
};

结果.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;
}
};

看看你的代码,你的#includes有点混乱。

ArrayOfThree.h标头中,即使您没有在ArrayOfThree.h标头中使用来自Result.h的任何代码,也要包含Result.h

另一方面,Result.h标头使用的是ArrayOfThree.h标头中定义的ArrayOfThree类,但Result.h不包含此标头。

您必须包含您正在使用的所有类、函数等的头文件。 因此,要解决您的问题,请将#include "ArrayOfThree.h"添加到Result.h标头中,然后从ArrayOfThree.h标头中删除#include "Result.h"


构建代码的更好方法是拆分类的声明和定义。这意味着您将有一个 TU(翻译单元(用于您的ArrayOfThree,以及Result课程。

例如,您的Result.h文件将变为:

#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();
};

您的项目Result.cpp中有一个新的TU,如下所示:

#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;
}

请注意,现在,您的Result.h标头不会#include <iostream>。这是因为Result.h标头不依赖于iostream中的任何内容,因为依赖于iostream的部分已移动到实现文件Result.cpp,其中包括iostream标头。