错误:"Left of getValue must have class/struct/union"
Error: "Left of getValue must have class/struct/union"
除了标题中的错误外,它还说A2
和A1
是未声明的标识符,并且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
标头。
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- C++Union/Struct位域的实现和可移植性
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 如何通过多类"Union variable" (sfml) 使用轮询事件
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- C++ class template
- 得到错误"left of '.toString' must have class/struct/union",即使 toString 应该有一个合适的对象
- [class.union]/3 注释中"Absent default member initializers, ..."的句子的目的是什么?
- 获取"Use of undefined type"和"Must have class/struct/union"错误
- 前向声明不够时如何避免"'identifier' uses undefined class/struct/union 'name'"错误?