Jump to content
  • Advertisement
Sign in to follow this  
Taint

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

This topic is 2068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++?

Share this post


Link to post
Share on other sites
Advertisement

 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.

Edited by Melkon

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites

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

That's using an incorrect symbol.

It should be ", not “

---

EDIT: Ninjaed by Brother Bob. Curses!

Edited by CoreLactose

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!