如何修复C++错误:预期的非限定 ID
How to fix C++ error: expected unqualified-id
我在第 6 行收到此错误:
error: expected unqualified-id before '{' token
我说不出出了什么问题。
#include <iostream>
using namespace std;
class WordGame;
{ // <== error is here on line 6
public:
void setWord( string word )
{
theWord = word;
}
string getWord()
{
return theWord;
}
void displayWord()
{
cout << "Your word is " << getWord() << endl;
}
private:
string theWord;
}
int main()
{
string aWord;
WordGame theGame;
cin >> aWord;
theGame.setWord(aWord);
theGame.displaymessage();
}
这里不应该有分号:
class WordGame;
。但是在类定义的末尾应该有一个:
...
private:
string theWord;
}; // <-- Semicolon should be at the end of your class definition
作为旁注,请考虑将 setWord() 中的字符串作为常量引用传递,以避免过度复制。 此外,在displayWord中,考虑将其设置为常量函数以遵循常量正确性。
void setWord(const std::string& word) {
theWord = word;
}
去掉WordGame
后的分号 .
当班级小得多时,你真的应该发现这个问题。当你在编写代码时,你应该在每次添加六行时进行编译。
号应该在类定义的末尾,而不是在名称之后:
class WordGame
{
};
对于它的价值,我遇到了同样的问题,但这不是因为额外的分号,而是因为我忘记了上一个语句中的分号。
我的情况是这样的
mynamespace::MyObject otherObject
for (const auto& element: otherObject.myVector) {
// execute arbitrary code on element
//...
//...
}
从这段代码中,我的编译器不断告诉我:
error: expected unqualified-id before for (const auto& element: otherObject.myVector) {
etc...
我认为这意味着我写错了 for 循环。不!我只是在宣布otherObject
后忘记了;
.
对于任何遇到这种情况的人: 当我不小心使用 my_first_scope::my_second_scope::true
代替简单的 true
时,我看到了此错误,如下所示:
bool my_var = my_first_scope::my_second_scope::true;
而不是:
bool my_var = true;
这是因为我有一个宏,导致MY_MACRO(true)
错误地扩展到my_first_scope::my_second_scope::true
,我实际上是在调用bool my_var = MY_MACRO(true);
。
下面是此类范围错误的快速演示:
程序(您可以在此处在线运行:https://onlinegdb.com/BkhFBoqUw):
#include <iostream>
#include <cstdio>
namespace my_first_scope
{
namespace my_second_scope
{
} // namespace my_second_scope
} // namespace my_first_scope
int main()
{
printf("Hello Worldn");
bool my_var = my_first_scope::my_second_scope::true;
std::cout << my_var << std::endl;
return 0;
}
输出(构建错误):
main.cpp: In function ‘int main()’: main.cpp:27:52: error: expected unqualified-id before ‘true’ bool my_var = my_first_scope::my_second_scope::true; ^~~~
请注意错误:error: expected unqualified-id before ‘true’
,以及错误下方的箭头指向的位置。显然,在我的情况下,"非限定 id"是我在true
之前使用的双冒号 (::
) 作用域运算符。
当我添加宏并使用它时(在此处运行此新代码:https://onlinegdb.com/H1eevs58D):
#define MY_MACRO(input) my_first_scope::my_second_scope::input
...
bool my_var = MY_MACRO(true);
我收到这个新错误:
main.cpp: In function ‘int main()’: main.cpp:29:28: error: expected unqualified-id before ‘true’ bool my_var = MY_MACRO(true); ^ main.cpp:16:58: note: in definition of macro ‘MY_MACRO’ #define MY_MACRO(input) my_first_scope::my_second_scope::input ^~~~~
我收到此错误是因为我没有声明变量并进一步使用它.这是我的代码,为什么我得到它。
这是因为我没有为>向量的大小声明变量。只需更换
int n=arr.size();
在这里替换,
int sumSubarrayMins(vector<int>& arr) {
int = arr.size();
long long sum;
long long ans =0;
for(long i =0;i<n;i++){
sum =0;
long mini=INT_MAX;
for(long long j =i;j<n;j++){
mini=min(mini,arr[j]);
sum+=mini;
}
ans+=sum;
}
return ans;
}
你忘记了这个空的setWord(字符串字)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Boost Graph Library,修复节点大小
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 如何修复函数样式强制转换或类型构造的预期"("?
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 当使用带有VS2019或VSCode的虚幻引擎4.24.2时,我如何修复这些错误的Intellisense错误
- 如何修复sfml c++代码编译错误
- Python中的for循环与C++有何不同
- 如何修复此iFile以将.txt输出到控制台
- 从函数角度看ID到文件路径的内部与外部映射
- 通过组合不同的类型来创建唯一的id
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 修复令牌错误之前';'预期的非限定 ID
- 如何修复 DevCC 编译器错误:[错误] 在"if"之前预期非限定 id?
- 即使我没有 #define 任何东西或错过分号,如何修复"expected unqualfied-id before string constant"?
- 错误:预期的非限定 ID 错误:含义和修复
- 如何修复C++错误:预期的非限定 ID