English 中文(简体)
如何确定C++的错误:预期的无保留补贴
原标题:How to fix C++ error: expected unqualified-id
  • 时间:2012-04-13 04:31:37
  •  标签:
  • c++

I m 将这一错误带入第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
问题回答

作为附带说明,考虑在“Word()”中添加插图,作为避免过份复制的参考资料。 此外,在显示Word中,考虑将这一职能置于最协调的基础之上。

void setWord(const std::string& word) {
  theWord = word;
}

Get rid of the semicolon after WordGame.

确实,当这个班子小得多时,你本应发现这个问题。 当你重写法典时,你应每增加一半。

Semicolon should be at the end of the class definition rather than after the name:

class WordGame
{
};

对于其价值来说,我有同样的问题,但由于有一个<斯特隆>extra的半殖民地,那是因为我忘记了先前发言中的一个半殖民地。

我的情况类似。

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... which I d taken to mean I d writtten the for loop wrong. Nope! I d simply forgotten a ; after declaring otherObject.

对有这种情况的人: 我在意外使用<密码>my_first_cop:my_second_cop:true取代单纯的true时,就看到了这一错误。

bool my_var = my_first_scope::my_second_scope::true;

instead of:

bool my_var = true;

这是因为我有一个宏观因素,导致<代码>MY_MACRO(true)扩大至my_first_cop:my_second_cop:true,错,我实际上叫<>bool my_var = MY_MACRO(true);。

这里是这种范围差错的快速解调:

方案(可上网查阅:):

#include <iostream>
#include <cstdio>

namespace my_first_scope 
{
namespace my_second_scope
{
} // namespace my_second_scope
} // namespace my_first_scope

int main()
{
    printf("Hello World
");
    
    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: 预期在“true'之前不合格,在错误下arrow指点。 www.un.org/Depts/DGACM/index_spanish.htm 范围 我刚刚在<代码>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
                                                          ^~~~~

I got this error because I was not declaring a variable and was using it further . Here is my code why I was getting it.

It was because I was not declaring a variable for size of my >vector. Just replace

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

您忘记了这一空白。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签