This is the forum to discuss the Wrox book Professional C++, 2nd Edition by Marc Gregoire, Nicholas A. Solter, Scott J. Kleper ; ISBN: 978-1-1181-6995-7
You are currently viewing the BOOK: Professional C++, 2nd Edition section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
Code::Blocks with -std=c++0x not accepting C++11 loops, pointers
I am using Code::Blocks 10.05 on Mint 11. For some reason, with the following code, I am not able to compile. If I comment out the C++11 range based for loop, it works fine aside from only giving me the output of the vector once.
Code:
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main(){
vector<string> myVector = {"A first string", "A second string"};
myVector.push_back("A third string");
myVector.push_back("The last string in the vector");
for(auto iterator = myVector.cbegin(); iterator != myVector.cend(); ++iterator)
cout << *iterator << endl;
for(auto& str : myVector)
cout << str << endl;
return 0;
}
Output is as follows:
Quote:
/home/ciphermagi/OOP/*** |In function ‘int main()’:|
/home/ciphermagi/OOP/*** |13|error: expected initializer before ‘:’ token|
/home/ciphermagi/OOP/*** |15|error: expected primary-expression before ‘return’|
/home/ciphermagi/OOP/*** |15|error: expected ‘;’ before ‘return’|
/home/ciphermagi/OOP/*** |15|error: expected primary-expression before ‘return’|
/home/ciphermagi/OOP/*** |15|error: expected ‘)’ before ‘return’|
||=== Build finished: 5 errors, 0 warnings ===|
I have also attempted to use the following without success:
Code:
shared_ptr<int> myInt(new int);
Resulting in:
Quote:
/home/ciphermagi/OOP/*** ||In function ‘int main()’:|
/home/ciphermagi/OOP/*** |8|error: ‘shared_ptr’ was not declared in this scope|
/home/ciphermagi/OOP/*** |8|error: expected primary-expression before ‘int’|
/home/ciphermagi/OOP/*** |8|error: expected ‘;’ before ‘int’|
||=== Build finished: 3 errors, 0 warnings ===|
I'm feeling like the whole C++11 library isn't installed, but I haven't been able to find a way to get a compiler that goes past GCC 4.5. Any suggestions?
[marc@marc-fedora13 c++0x]$ cat range.cpp
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main(){
vector<string> myVector = {"A first string", "A second string"};
myVector.push_back("A third string");
myVector.push_back("The last string in the vector");
for(auto iterator = myVector.cbegin(); iterator != myVector.cend(); ++iterator)
cout << *iterator << endl;
for(auto& str : myVector)
cout << str << endl;
return 0;
}
[marc@marc-fedora13 c++0x]$ /media/Disk2/gcc-4.6-20110205/bin/g++ -std=c++0x range.cpp
[marc@marc-fedora13 c++0x]$ ./a.out
A first string
A second string
A third string
The last string in the vector
A first string
A second string
A third string
The last string in the vector
[marc@marc-fedora13 c++0x]$
Thanks for the blazing fast response. I do see that there is available a minGW build for gcc 4.6.1, but I was hoping for something that's not designed specifically for Windows. There doesn't seem to be any support for any compilers in linux under that thread, and I would prefer not to have to use wine just to be able to write code.
I would be willing to change IDE or even go with no IDE at all and just use command line linking if I can find something that stays away from having to emulate another OS in order to do this work.
I don't have much experience with Linux myself, but you should be able to install a new GCC package on your distribution.
If I look at http://community.linuxmint.com/software/view/gcc I see something called "Lisa (4:4.6.1-2ubuntu5)", maybe that's the GCC 4.6 compiler?
If not, you either need to find a 4.6 package somewhere for your linux distribution, or download the GCC source from http://gcc.gnu.org/ and compiler it yourself.
Actually, that's not up to date for 4.6, however I did find another person with Linux experience that was willing to point me in the right direction. In the interest of free information, here it is:
Edit:
Upon following the above instructions (and making changes as needed for version numbers) I am getting the correct output for the above range-based for loop.
Last edited by ciphermagi; January 19th, 2012 at 07:42 PM.
Reason: Additional Information