[C++, Linux] Can't compile correct code

Started by
8 comments, last by Serapth 10 years, 2 months ago

I'm using the g++ compiler and it can handle the basic stuff, but as soon as I try to use pointers it gives me errors. I 100% know the code is correct because I even pasted code from C++ Primer 5th Edition into it and it still isn't working. I did download a load of dev libraries when trying to install SFML so I'm thinking maybe that did it, but I don't know. I also don't have the slightest idea how to reverse it.

This is the code I pasted from C++ Primer 5th Edition, which apparently is the best book for learning. I've tried compiling it with g++ main.cpp -o main, and also with the -std=C++11 flag.


#include <iostream>
int main()
{
	using namespace std;
	int updates = 6;
// declare a variable
	int * p_updates;
// declare pointer to an int
	p_updates = &updates;
// assign address of int to pointer
// express values two ways
	cout << “Values: updates = “ << updates;
	cout << “, *p_updates = “ << *p_updates << endl;
// express address two ways
	cout << “Addresses: &updates = “ << &updates;
	cout << “, p_updates = “ << p_updates << endl;
// use pointer to change value
	*p_updates = *p_updates + 1;
	cout << “Now updates = “ << updates << endl;
	return 0;
}

Why does this happen every time I try and learn anything new in C++?

Advertisement

You should also post the error, so we could know what exactly went wrong.

I 100% know the code is correct

Well, i work as a c++ developer, and i am never brave enough to say that when i have a compile error. smile.png

At first look it seems fine for me but... please, write the compile error, because it's much more easier to help if we see what's your problem.

Sorry guys! This is the error:


 g++ main.cpp -o main
main.cpp:12:2: error: stray ‘\342’ in program
  cout << “Values: updates = “ << updates;
  ^
main.cpp:12:2: error: stray ‘\200’ in program
main.cpp:12:2: error: stray ‘\234’ in program
main.cpp:12:2: error: stray ‘\342’ in program
main.cpp:12:2: error: stray ‘\200’ in program
main.cpp:12:2: error: stray ‘\234’ in program
main.cpp:13:2: error: stray ‘\342’ in program
  cout << “, *p_updates = “ << *p_updates << endl;
  ^
main.cpp:13:2: error: stray ‘\200’ in program
main.cpp:13:2: error: stray ‘\234’ in program
main.cpp:13:2: error: stray ‘\342’ in program
main.cpp:13:2: error: stray ‘\200’ in program
main.cpp:13:2: error: stray ‘\234’ in program
main.cpp:15:2: error: stray ‘\342’ in program
  cout << “Addresses: &updates = “ << &updates;
  ^
main.cpp:15:2: error: stray ‘\200’ in program
main.cpp:15:2: error: stray ‘\234’ in program
main.cpp:15:2: error: stray ‘\342’ in program
main.cpp:15:2: error: stray ‘\200’ in program
main.cpp:15:2: error: stray ‘\234’ in program
main.cpp:16:2: error: stray ‘\342’ in program
  cout << “, p_updates = “ << p_updates << endl;
  ^
main.cpp:16:2: error: stray ‘\200’ in program
main.cpp:16:2: error: stray ‘\234’ in program
main.cpp:16:2: error: stray ‘\342’ in program
main.cpp:16:2: error: stray ‘\200’ in program
main.cpp:16:2: error: stray ‘\234’ in program
main.cpp:19:2: error: stray ‘\342’ in program
  cout << “Now updates = “ << updates << endl;
  ^
main.cpp:19:2: error: stray ‘\200’ in program
main.cpp:19:2: error: stray ‘\234’ in program
main.cpp:19:2: error: stray ‘\342’ in program
main.cpp:19:2: error: stray ‘\200’ in program
main.cpp:19:2: error: stray ‘\234’ in program
main.cpp: In function ‘int main()’:
main.cpp:12:19: error: found ‘:’ in nested-name-specifier, expected ‘::’
  cout << “Values: updates = “ << updates;
                   ^
main.cpp:12:13: error: ‘Values’ has not been declared
  cout << “Values: updates = “ << updates;
             ^
main.cpp:12:35: error: expected primary-expression before ‘<<’ token
  cout << “Values: updates = “ << updates;
                                   ^
main.cpp:13:13: error: expected primary-expression before ‘,’ token
  cout << “, *p_updates = “ << *p_updates << endl;
             ^
main.cpp:13:32: error: expected primary-expression before ‘<<’ token
  cout << “, *p_updates = “ << *p_updates << endl;
                                ^
main.cpp:15:13: error: ‘Addresses’ was not declared in this scope
  cout << “Addresses: &updates = “ << &updates;
             ^
main.cpp:15:22: error: expected ‘;’ before ‘:’ token
  cout << “Addresses: &updates = “ << &updates;
                      ^
main.cpp:16:13: error: expected primary-expression before ‘,’ token
  cout << “, p_updates = “ << p_updates << endl;
             ^
main.cpp:16:31: error: expected primary-expression before ‘<<’ token
  cout << “, p_updates = “ << p_updates << endl;
                               ^
main.cpp:19:13: error: ‘Now’ was not declared in this scope
  cout << “Now updates = “ << updates << endl;
             ^
main.cpp:19:17: error: expected ‘;’ before ‘updates’
  cout << “Now updates = “ << updates << endl;

You're using the wrong quote marks. You're using “ while the correct mark is ".

cout << “Values: updates = “ << updates;

That's using an incorrect symbol.

It should be ", not “

---

EDIT: Ninjaed by Brother Bob. Curses!

Hello to all my stalkers.

Edit: Nvm, others answered it already.

Brilliant, thank you.

A colleague was recently befuddled why a simple command line argument wasn't working. He was passing in -n as an argument, copied straight from an email.

Well, it turned out, some email editor (Outlook) had changed the "-" to some longer single character - (possibly UNICODE) and it was screwing up the argument.

Lesson is, be careful when copying and pasting; sometimes it doesn't give you exactly what you expect.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

A colleague was recently befuddled why a simple command line argument wasn't working. He was passing in -n as an argument, copied straight from an email.

Well, it turned out, some email editor (Outlook) had changed the "-" to some longer single character - (possibly UNICODE) and it was screwing up the argument.

Lesson is, be careful when copying and pasting; sometimes it doesn't give you exactly what you expect.

I had a similar experience in my ancient history, pasting code from word. MIcrosoft apparently have a "smart quote" character, and non-Visual Studio compilers really weren't happy with it.

This topic is closed to new replies.

Advertisement