Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Professional C++, 2nd Edition
|
BOOK: Professional C++, 2nd Edition
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
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C++, 2nd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 13th, 2012, 02:03 PM
Registered User
 
Join Date: Jan 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?
 
Old January 13th, 2012, 02:11 PM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

With GCC 4.6 I get the following:

Code:
[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]$
See if the following helps:
http://forums.codeblocks.org/index.php?topic=14942.0
 
Old January 13th, 2012, 02:33 PM
Registered User
 
Join Date: Jan 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old January 13th, 2012, 03:03 PM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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.
 
Old January 19th, 2012, 08:41 PM
Registered User
 
Join Date: Jan 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:

http://buildall.wordpress.com/2011/0...-ubuntu-10-10/

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 08:42 PM.. Reason: Additional Information
 
Old January 20th, 2012, 03:20 AM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

That's good to hear :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList keyword not accepting in the Code pabnix WinForms/Console Application Design 2 February 24th, 2008 11:22 AM
break for-each loops, or limit amount of loops warhero XSLT 2 July 4th, 2007 02:18 AM
While loops and For loops in XSLT spencer.clark XSLT 1 August 5th, 2005 09:50 AM
Commenting large blocks of code goplayoutside VB.NET 2002/2003 Basics 2 April 21st, 2004 04:17 PM
disable forum code within [code] blocks? nikolai Forum and Wrox.com Feedback 0 October 23rd, 2003 07:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.