Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Professional C++, 2nd Edition
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 13th, 2012, 01:03 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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?
Reply With Quote
  #2 (permalink)  
Old January 13th, 2012, 01:11 PM
Wrox Author
Points: 139, Level: 2
Points: 139, Level: 2 Points: 139, Level: 2 Points: 139, Level: 2
Activity: 18%
Activity: 18% Activity: 18% Activity: 18%
 
Join Date: Mar 2011
Posts: 26
Thanks: 0
Thanked 3 Times in 3 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
Reply With Quote
  #3 (permalink)  
Old January 13th, 2012, 01:33 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #4 (permalink)  
Old January 13th, 2012, 02:03 PM
Wrox Author
Points: 139, Level: 2
Points: 139, Level: 2 Points: 139, Level: 2 Points: 139, Level: 2
Activity: 18%
Activity: 18% Activity: 18% Activity: 18%
 
Join Date: Mar 2011
Posts: 26
Thanks: 0
Thanked 3 Times in 3 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.
Reply With Quote
  #5 (permalink)  
Old January 19th, 2012, 07:41 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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 07:42 PM. Reason: Additional Information
Reply With Quote
  #6 (permalink)  
Old January 20th, 2012, 02:20 AM
Wrox Author
Points: 139, Level: 2
Points: 139, Level: 2 Points: 139, Level: 2 Points: 139, Level: 2
Activity: 18%
Activity: 18% Activity: 18% Activity: 18%
 
Join Date: Mar 2011
Posts: 26
Thanks: 0
Thanked 3 Times in 3 Posts
Default

That's good to hear :)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 10: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



All times are GMT -4. The time now is 07:17 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.