Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
Oh, yes you will. At the worst possible moment. I am not a professional programmer, but after years of programming I still sometimes forget the second =. I am pretty sure that everyone does. It happens very rarely, but when it does you don't get a compilation error. You only get obviously wrong results and in a sufficiently complex program the cause could be in a million different places, so I hope you like debugging.

A trick that some people do is to reverse the order of the comparison. So instead of 'if(a==4)', you would write 'if(4==a)'. Now if you forget the second = the compiler will get 'if(4=a)' and will throw an error (this works only if you compare with constants). I personally don't like this, since I find it decreases the code readability, but you may find it useful.

Yeah, you are probably right. :eek:

Thanks for the hint, I might give that a try. ;)
 
OK, got a fresh problem for the experts here. :) As I've said, the book I'm using is quite old. Usually I'm able to determine what works and what doesn't. But an example related to global and local variables has me stumped. I'm afraid I may be missing something that's out-of-date or otherwise unsupported by the latest standards. :eek: Or a simple syntax error I can't see to save my life. :rolleyes:

g++ kicks back the following errors:

Code:
$ g++ -o globalvar globalvar.cpp
globalvar.cpp: In function ‘int main()’:
globalvar.cpp:26: error: ‘count’ was not declared in this scope
globalvar.cpp: In function ‘void func1()’:
globalvar.cpp:35: error: ‘count’ was not declared in this scope

The source code:

Code:
 /*
 *  globalvar.cpp
 *  
 *
 *  Created by [redacted] on 2/20/10.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */

//This program demostrates global variables

#include <iostream>

using namespace std;

void func1();
void func2();

int count; //global variable

main()
{
	int x; //local variable
	
	for(x=0; x<10; x++) {
		count = x * 2;
		func1();
	}
	
	return 0;
}

void func1()
{
	cout << "count: " << count; //access global count
	cout << '\n';
	func2();
}

void func2()
{
	int count; //local variable
	
	for(count=0; count<3; count++) cout << '.';
}

[edit] I'm pretty certain the "=" operators are correct here; that's the way the example is written in the book.
 
I don't know why your error messages are so terse (maybe it's a GCC version thing), but here's what I got when I tried to compile your code--it's a LOT more informative.


Code:
[Blackie:~/Documents] (0) whschultz% g++ globalvar.cpp 
globalvar.cpp: In function ‘int main()’:
globalvar.cpp:26: error: reference to ‘count’ is ambiguous
globalvar.cpp:19: error: candidates are: int count
/usr/include/c++/4.2.1/bits/stl_algo.h:424: error:                 template<class _InputIterator, class _Tp> typename std::iterator_traits::difference_type std::count(_InputIterator, _InputIterator, const _Tp&)
globalvar.cpp:26: error: reference to ‘count’ is ambiguous
globalvar.cpp:19: error: candidates are: int count
/usr/include/c++/4.2.1/bits/stl_algo.h:424: error:                 template<class _InputIterator, class _Tp> typename std::iterator_traits::difference_type std::count(_InputIterator, _InputIterator, const _Tp&)
globalvar.cpp: In function ‘void func1()’:
globalvar.cpp:35: error: reference to ‘count’ is ambiguous
globalvar.cpp:19: error: candidates are: int count
/usr/include/c++/4.2.1/bits/stl_algo.h:424: error:                 template<class _InputIterator, class _Tp> typename std::iterator_traits::difference_type std::count(_InputIterator, _InputIterator, const _Tp&)
globalvar.cpp:35: error: reference to ‘count’ is ambiguous
globalvar.cpp:19: error: candidates are: int count
/usr/include/c++/4.2.1/bits/stl_algo.h:424: error:                 template<class _InputIterator, class _Tp> typename std::iterator_traits::difference_type std::count(_InputIterator, _InputIterator, const _Tp&)


and for reference:

Code:
[Blackie:~/Documents] (1) whschultz% g++ --version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646) (dot 1)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Anyway, this is a perfect example for why global variables tend to be considered a Bad Thing™. You try to reference it as "count," but that has already been used. Try something less arbitrary, like "counting_sheep."

EDIT: I got the same error as you using "g++-4.0".


EDIT2: Changing the name of "count" to "counting_sheep" worked for me, and I got this:

Code:
[Blackie:~/Documents/test] (0) whschultz% ./a.out 
counting_sheep: 0
...counting_sheep: 2
...counting_sheep: 4
...counting_sheep: 6
...counting_sheep: 8
...counting_sheep: 10
...counting_sheep: 12
...counting_sheep: 14
...counting_sheep: 16
...counting_sheep: 18
 
Thanks! Seems my version of g++ is a bit out of date:
Code:
$ g++ -v
Using built-in specs.
Target: i686-apple-darwin9
Configured with: /var/tmp/gcc/gcc-5493~1/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin9 --with-arch=apple --with-tune=generic --host=i686-apple-darwin9 --target=i686-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5493)

However, I will be upgrading my MBP (which the computer I primarily use for this) to Snow Leopard this summer, and that will mean Xcode 3.2 which I assume will have newer compilers. Perhaps there's also an option for verbose output...wait, you didn't use the -o option, so maybe that's it!

I will make recommended adjustments to see what happens. Thanks for compiling it for me to get a better error message. :)
 
By changing "count" to "counting_sheep" I got the same result you did, with no errors.
 
Just because GCC 4.0 is your default doesn't mean you can't use GCC 4.2. It's been part of the developer tools since at least 10.5 (maybe 10.4?). I thought there was a "gcc-select" tool to change the default, but I'm not seeing it in 10.6. Regardless, you can still use "gcc-4.2" at the command line.
 
Just because GCC 4.0 is your default doesn't mean you can't use GCC 4.2. It's been part of the developer tools since at least 10.5 (maybe 10.4?). I thought there was a "gcc-select" tool to change the default, but I'm not seeing it in 10.6. Regardless, you can still use "gcc-4.2" at the command line.

Thanks for the hint! :)
 
Hey guys!!! No, I haven't dropped off the face of the earth. :D

I was wondering, since this thread is a bit old now, what you guys thought was the best book for C++ out there? I have an old book as I stated above, but it's hopelessly out of date. :(

Thanks!
 
If you seriously feel that printing out the contents of a single variable, along with the fact that it takes 1 byte in memory, is fun... then you have a bright future ahead in programming.

I'm serious.

I've seen so many times that beginners think they'll start off with writing a simple 3D first person shooter ("a simple one, without network playing and all that") and then be seriously desillusioned after the first few days.

But let's be clear about it: if you consider what a huge amount of stuff is going on inside your computer when you run that simple first program, it's mind boggling. And fun!

I agree with this. Its really bad. I wont say which forum but a popular gaming forum there are people who have used c++ for less then 3 months asking for help for a mmorpg. not even a regular rpg. but a mmorpg lol.

If a single person would ever attempt a mmorpg is tells exactly how much they dont know.
 
I'm pleased to say that I'm taking a class in C this fall. I hope that I will take a lot away from it; I really like the instructor, who has 30+ years of experience as a programmer.

Also, I got me a nice load of up-to-date books on C and C++ (as well as shell scripting), as well as some of the O'Reilly pocket references.
 
Oh, and rest assured I won't be asking for help with homework under the guise of being a total noob. :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.