IntelliSense:该对象具有与成员函数不兼容的类型限定符
IntelliSense: the object has type qualifiers that are not compatible with the member function
我有一个叫人的类:
class Person {
string name;
long score;
public:
Person(string name="", long score=0);
void setName(string name);
void setScore(long score);
string getName();
long getScore();
};
在另一堂课中,我有此方法:
void print() const {
for (int i=0; i< nPlayers; i++)
cout << "#" << i << ": " << people[i].getScore()//people is an array of person objects
<< " " << people[i].getName() << endl;
}
这是人的声明:
static const int size=8;
Person people[size];
当我尝试编译它时,我会收到此错误:
IntelliSense: the object has type qualifiers that are not compatible with the member function
在2 下的红线[I] 在打印方法
中我在做什么错?
getName
不是const, getScore
不是const,而是 print
是。使前两个const像print
。您不能用const对象调用非const方法。由于您的个人对象是您的其他班级的直接成员,并且由于您使用的是const方法,因此被认为是const。
通常,您应该考虑您编写的每种方法并将其声明为const。诸如getScore
和getName
之类的简单获取器应始终为const。
相关文章:
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 字符类型转换不兼容
- 视觉工作室 2017;启用 /permissive 时,类型 "const wchar_t *" 的参数与类型 "PWSTR" 的参数不兼容
- 类型为 "int*" 的参数与 C++ 中错误类型"int**"参数不兼容
- 该对象具有与成员函数不兼容的类型限定符.为什么会出现此错误?
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- C++ 类型的参数与 void (__cdecl*)(void) 类型的参数不兼容,当调用 std::atexit()
- 将"std::string {aka std::basic_string}"赋值中的不兼容类型<char>
- OPENCL 警告:不兼容的指针类型将'float __global[16]'传递给类型为 '__global float4 的参数 *
- 如何修复"方法的类型与 PInvoke 不兼容"
- C++不兼容的迭代器类型
- 编译器错误"在if语句中分配不兼容的类型"
- 程序无法编译:将 'int (*)[3][3]' 赋值为 'int [9][3][3]' 中的不兼容类型
- 将"float*"赋值为"float* [1000]"中的不兼容类型
- 在C 中,是否有可能在不兼容类型的std ::向量对象之间传输不同类型的缓冲区
- 赋值null中的不兼容类型
- 数组到数组 -- 将'int*'分配给 'int 错误C++中的不兼容类型
- 对于将 const char 分配给 std::string 的这种代码不兼容类型的错误,我不断收到此错误
- (Dev-c++) 将'int'分配给'char[1]'的不兼容类型
- C++不兼容类型:计算等位基因频率