expected initializer before ‘:’ token - c++

My code looks likes below:
#include <vector>
#include <iostream>
#include <map>
using namespace std;
int main()
{
std::map<int, std::vector<int> > map;
map[1].push_back(5);
map[1].push_back(3);
map[3].push_back(2);
map[2].push_back(1);
map[1].push_back(-1);
map[3].push_back(2);
int sum2 = 0;
for (const pair<int, vector<int> >& index_vec : map)
{
int sum = 0;
for (int elem : index_vec.second)
{
sum += elem;
}
sum2 += sum*sum;
cout << "index " << index_vec.first << ": " << sum << endl;
}
cout << "sum_2: " << sum2 << endl;
return 0;
};
Which works fine in my laptop but when using desktop gives me following erors:
map.cpp: In function ‘int main()’:
map.cpp:17: error: expected initializer before ‘:’ token
map.cpp:29: error: expected primary-expression at end of input
map.cpp:29: error: expected ‘;’ at end of input
map.cpp:29: error: expected primary-expression at end of input
map.cpp:29: error: expected ‘)’ at end of input
map.cpp:29: error: expected statement at end of input
map.cpp:29: error: expected ‘}’ at end of input
The output should be:
index 1: 7
index 2: 1
index 3: 4
sum_2: 66
which my laptop gives as expected. I have absolutely no idea, could soeone please help me out?

Your code works on me without any modification. Please check your compiler. You will need a C++11 compiler. If you use g++, something like this:
g++ -std=c++11 A.cpp
https://gcc.gnu.org/projects/cxx-status.html#cxx11. You need at least 4.7. Please update your ancient compiler.

Related

C++ - Strange problem with loop initialization

The compiler can't handle even the simplest loop
#include <iostream>
using namespace::std;
int main()
{
for( int i = 0, char a = 'A'; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Compiler says this:
prog.cpp: In function ‘int main()’:
prog.cpp:7:18: error: expected unqualified-id before ‘char’
prog.cpp:7:18: error: expected ‘;’ before ‘char’
prog.cpp:7:39: error: expected ‘)’ before ‘;’ token
prog.cpp:7:41: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
prog.cpp:7:41: note: (if you use ‘-fpermissive’ G++ will accept your code)
prog.cpp:7:46: error: ‘a’ was not declared in this scope
prog.cpp:7:50: error: expected ‘;’ before ‘)’ token
And yes, I know I can initialize 'i' and 'a' before the loop. So let's try:
#include <iostream>
using namespace::std;
int main()
{
int i = 0;
for(i = 0, char a = 'A'; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Compiler says:
prog.cpp: In function ‘int main()’:
prog.cpp:8:13: error: expected primary-expression before ‘char’
prog.cpp:8:13: error: expected ‘;’ before ‘char’
prog.cpp:8:41: error: ‘a’ was not declared in this scope
When option -std=c++11 used:
prog.cpp: In function ‘int main()’:
prog.cpp:7:17: error: expected unqualified-id before ‘char’
prog.cpp:7:17: error: expected ‘;’ before ‘char’
prog.cpp:7:38: error: expected ‘)’ before ‘;’ token
prog.cpp:7:40: error: ‘i’ was not declared in this scope
prog.cpp:7:45: error: ‘a’ was not declared in this scope
prog.cpp:7:49: error: expected ‘;’ before ‘)’ token
Last one:
#include <iostream>
using namespace::std;
int main()
{
int i = 0;
char a = 'A';
for(i = 0, a = 'A'; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Works fine. Guys, am I blind or something? Maybe you need my arch and compiler version:
uname -a
Linux freelancer 3.2.0-4-686-pae #1 SMP Debian 3.2.63-2+deb7u2 i686 GNU/Linux
g++ --version
g++ (Debian 4.7.2-5) 4.7.2
You cannot declare items of different types in the same declaration.
This is true inside and outside of loops. You're not "blind", it's just not valid C++.
Here's the right code:
#include <iostream>
using namespace::std;
int main()
{
int i = 0;
char a = 'A';
for(; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Your working version is also valid because the declaration can be swapped out for an expression, though in your case it's redundant because those variables already hold those values at the start of the loop.
26 is so tiny number, thus you can do
#include <iostream>
int main()
{
for( char i = 0, a = 'A'; i <= 26; i++, a++ )
std::cout << "OK, lets try. Showing values: i = "
<< static_cast<int>(i) << ", a = " << a << std::endl;
}
Or even IMHO more clear code, more clear aim, iterating from 'A' until 'Z'.
int main()
{
for( char i = 0, a = 'A'; a <= 'Z'; i++, a++ )
std::cout << "OK, lets try. Showing values: i = "
<< static_cast<int>(i) << ", a = " << a << std::endl;
}

Code Blocks 13.12 C++ basic for loop debug error

I am coding in C++ in Code Blocks 13.12 with TDM-GCC compiler version 4.8.1 on Windows 10 64-bit, but while in any other PC I have used (both at school and university wtih Win7 64-bit and Win8 64-bit, respectively) there were no problems with debugging and running programs, in my case I am not even able to compile simple "for" loop. I tried all versions of CB, withou success.
Here is the siplest code, that gives errors:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
for (int i = 0,i < 9,i++)
cout << "Test, ",i << endl;
return 0;
}
As you can see, it is a sightly modified basic console application in C++.
The errors I get are following:
error: expected initializer before '<' token
error: expected ';' before '<' token
error: expected primary-expression before '<' token
error: expected ';' before ')' token
error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
I tried everything - form installing different versions of code blocks, to disableing anti-virus software.
Can you tell me what have I been doing wrong?
Your for loop syntax is wrong.
It should be:
for (int i = 0; i < 9; ++i) and also add the {} after the ).
And also
cout << "Test, " <<i << endl; //and also the concatenation was wrong
Hope it helps.
The curly braces are not necessary if the loop body contains only one expression.
You should change commas to semicolons between the braces after "for". "for" is not a function, it is a keyword and the statement "int i=0" is not a parameter.
Here is the right code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
for (int i = 0; i < 9; i++)
cout << "Test, " << i << endl;
return 0;
}

ERROR: slist was not declared in this scope

This code isn't working for me, I am getting below-mentioned compilation errors.
Somebody please help me to point out my mistake.
In function 'int main()':
4:3: error: 'slist' was not declared in this scope
4:9: error: expected primary-expression before 'int'
5:3: error: 'L' was not declared in this scope
12:9: error: expected primary-expression before 'int'
13:3: error: 'back' was not declared in this scope
Code:
int main() {
slist<int> L;
L.push_front(0);
L.push_front(1);
L.insert_after(L.begin(), 2);
copy(L.begin(), L.end(), // The output is 1 2 0
ostream_iterator<int>(cout, " "));
cout << endl;
slist<int>::iterator back = L.previous(L.end());
back = L.insert_after(back, 3);
back = L.insert_after(back, 4);
back = L.insert_after(back, 5);
copy(L.begin(), L.end(), // The output is 1 2 0 3 4 5
ostream_iterator<int>(cout, " "));
cout << endl;
}
All the problems boil down to the fact that the following declaration
slist<int> L;
throwed the error that
slist was not declared in this scope
You need to make sure slist is declared in your scope. include the necessary headers.

Errors in a sqrt function program in c++

**Essentially I was given pseudo code:
"x = 1
repeat 10 times: x = (x + n / x) / 2
return x"
And the pseudo code for the int main function (int main function to print out my n values in the cout) at the end, in order to create a sqrt function program. I get the following errors on linux2 compiler:
: In function ‘double my_sqrt_1(double)’:
:9:1: error: expected primary-expression before ‘return’
:9:1: error: expected ‘;’ before ‘return’
: In function ‘int main()’:
:
15:13: error: expected unqualified-id before ‘-’ token
:~> expected primary-expression before ‘return’
Help is much appreciated!
#include <iostream>
#include <math.h>
using namespace std;
double my_sqrt_1(double n)
{
for (int x= 1; x<10; ++x)
cout<< x << '\t' << x=(x+n/x)/2 <<
return x;
}
int main()
{
int n= 3.141459;
int k= -100,-10,-1,0,1,10,and 100;
for(auto k : { -100,-10,-1,0,1,10,100}){
n=3.14159 * pow (10.0,k);
cout << "print n,sqrt(n),and my_sqrt_1(n)" ;
return 0;
}
}
You missed a semicolon at the end of the cout line:
double my_sqrt_1(double n)
{
for (int x= 1; x<10; ++x)
cout<< x << '\t' << x=(x+n/x)/2;
return x;
}
The clue is in the error:
:9:1: error: expected ‘;’ before ‘return’
Finding the source of compiler errors can be tricky for those new to C/C++, if you miss a semi-colon the line reported will often differ from the one containing the actual error. As in this case where the return line became part of the same statement as the line above.
Also here:
int k= -100,-10,-1,0,1,10,and 100;
That is not how you define an array, you should read up on the basics of those since you're new to the game, which is evident here:
cout << "print n,sqrt(n),and my_sqrt_1(n)" ;
Where you're not calling any functions but instead outputting a static string of text. You need to make the function calls and variable outputs outside of the literal string:
cout << "print " << n << "," << sqrt(n) << ", and" << my_sqrt_1(n);

Why am I getting the error "cin does not name a type"

While trying to write this code, I get an error "cin doesnt name a type".
I don't know what the problem is exactly and I tried to write "using namespace std;"
but it gave the same error.
Here's the code
#include<iostream>
namespace myStuff {
int value = 0;
}
using namespace myStuff;
int main {
std::cout << "enter integer " << ;
std::cin >> value;
std::cout << "\nyouhaveenterd a value" << value ;
return 0;
}
Here's the compilation error :
: extended initializer lists only available with `-std=c++0x` or `-std=gnu++0x` [enabled by default]|
: expected primary-expression before ‘;’ token|
expected `}` before `;` token|
`cin` does not name a type|
: `cout` does not name a type|
: expected unqualified-id before `return`|
: expected declaration before `}` token|
||=== Build finished: 6 errors, 1 warnings ===|
int main{
should be
int main(){
and
std::cout << "enter integer " << ;
should be
std::cout << "enter integer ";
On this line:
std::cout << "enter integer " << ;
There's no corresponding operand to make the statement syntactically valid. That's probably the source of your errors.
Its the previous line.
cout<<"enter integer" **<<** ;
that last << is expecting an argument which is never given