Reputation: 2537
I am just trying to familiarise myself with the basics of C++ moving from Java. I just wrote this functionality abstinent program and am coming across an errortest.cpp:15: error: expected primary-expression before ‘<<’ token
and I am not sure why.
Anybody care to explain why endl
is not working with constants? The code is below.
//Includes to provide functionality.
#include <iostream>
//Uses the standard namespace.
using namespace std;
//Define constants.
#define STRING "C++ is working on this machine usig the GCC/G++ compiler";
//Main function.
int main()
{
string enteredString;
cout << STRING << endl;
cout << "Please enter a String:" << endl;
cin >> enteredString;
cout << "Your String was:" << endl;
cout << enteredString << endl;
return(0);
}
Upvotes: 0
Views: 463
Reputation: 7118
Upvotes: 0
Reputation: 643
Remove ; from
#define STRING "C++ is working on this machine usig the GCC/G++ compiler"
Upvotes: 1
Reputation: 17186
You have a ;
in your preprocessor definition. Note that #DEFINE STRING x
just copies the whole x-statement (including the ;
) into the place where it's referenced.
Also, a preprocessor constant isn't a language constant. You should use const string STRING("C++ is working on this machine usig the GCC/G++ compiler");
Upvotes: 6
Reputation: 3509
You've got a secmi-colon at the end of your #define
- this will be substituted into your code, giving.
cout << "C++ is working on this machine usig the GCC/G++ compiler"; << endl;
Upvotes: 2
Reputation: 4032
Because you have a semi colon after STRING. Remove it and give it a try...
Upvotes: 1
Reputation: 139890
Your #define
has a semicolon at the end. That becomes part of the macro, so the pre-processed code looks like this:
cout << "C++ is working on this machine usig the GCC/G++ compiler"; << endl;
Remove the semicolon and you should be fine.
PS: It's usually a better idea to use real constants for this rather than relying on the preprocessor:
const char *STRING = "C++ is working on this machine usig the GCC/G++ compiler";
Upvotes: 8