Reputation: 2016
I'm getting this error on line 6:
error: expected unqualified-id before '{' token
I can't tell what's wrong.
#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();
}
Upvotes: 61
Views: 412878
Reputation: 1726
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();
Replace Here,
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;
}
Upvotes: 0
Reputation: 52677
For anyone with this situation: I saw this error when I accidentally used my_first_scope::my_second_scope::true
in place of simply true
, like this:
bool my_var = my_first_scope::my_second_scope::true;
instead of:
bool my_var = true;
This is because I had a macro which caused MY_MACRO(true)
to expand into my_first_scope::my_second_scope::true
, by mistake, and I was actually calling bool my_var = MY_MACRO(true);
.
Here's a quick demo of this type of scoping error:
Program (you can run it online here: 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 World\n");
bool my_var = my_first_scope::my_second_scope::true;
std::cout << my_var << std::endl;
return 0;
}
Output (build error):
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; ^~~~
Notice the error: error: expected unqualified-id before ‘true’
, and where the arrow under the error is pointing. Apparently the "unqualified-id" in my case is the double colon (::
) scope operator I have just before true
.
When I add in the macro and use it (run this new code here: https://onlinegdb.com/H1eevs58D):
#define MY_MACRO(input) my_first_scope::my_second_scope::input
...
bool my_var = MY_MACRO(true);
I get this new error instead:
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 ^~~~~
Upvotes: 0
Reputation: 3512
For what it's worth, I had the same problem but it wasn't because of an extra semicolon, it was because I'd forgotten a semicolon on the previous statement.
My situation was something like
mynamespace::MyObject otherObject
for (const auto& element: otherObject.myVector) {
// execute arbitrary code on element
//...
//...
}
From this code, my compiler kept telling me:
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
.
Upvotes: 2
Reputation: 443
Semicolon should be at the end of the class definition rather than after the name:
class WordGame
{
};
Upvotes: 3
Reputation: 1903
As a side note, consider passing strings in setWord() as const references to avoid excess copying. Also, in displayWord, consider making this a const function to follow const-correctness.
void setWord(const std::string& word) {
theWord = word;
}
Upvotes: 9
Reputation: 2590
There should be no semicolon here:
class WordGame;
...but there should be one at the end of your class definition:
...
private:
string theWord;
}; // <-- Semicolon should be at the end of your class definition
Upvotes: 44
Reputation: 99094
Get rid of the semicolon after WordGame
.
You really should have discovered this problem when the class was a lot smaller. When you're writing code, you should be compiling about every time you add half a dozen lines.
Upvotes: 7