Not working on Ubuntu

Pages: 12
Hi there
Compiler G++
I recently switched to Ubuntu from Windows 8.I downloaded this code from a link that was in my book (C++ priner 5th edition)

This works on windows but not linux ...its a .h (Header)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>

class Sales_item {
// these declarations are explained section 7.2.1, p. 270
// and in chapter 14, pages 557, 558, 561
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool
operator==(const Sales_item&, const Sales_item&);
public:
    // constructors are explained in section 7.1.4, pages 262 - 265
    // default constructor needed to initialize members of built-in type
    Sales_item() = default;
    Sales_item(const std::string &book): bookNo(book) { }
    Sales_item(std::istream &is) { is >> *this; }
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);

    // operations on Sales_item objects
    std::string isbn() const { return bookNo; }
    double avg_price() const;
// private members as before
private:
    std::string bookNo;      // implicitly initialized to the empty string
    unsigned units_sold = 0; // explicitly initialized
    double revenue = 0.0;
};

// used in chapter 10
inline
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{ return lhs.isbn() == rhs.isbn(); }

// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
    // must be made a friend of Sales_item
    return lhs.units_sold == rhs.units_sold &&
           lhs.revenue == rhs.revenue &&
           lhs.isbn() == rhs.isbn();
}

inline bool
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
    return !(lhs == rhs); // != defined in terms of operator==
}

// assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& rhs)
{
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;
}

// assumes that both objects refer to the same ISBN
Sales_item
operator+(const Sales_item& lhs, const Sales_item& rhs)
{
    Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll return
    ret += rhs;           // add in the contents of (|rhs|)
    return ret;           // return (|ret|) by value
}

std::istream&
operator>>(std::istream& in, Sales_item& s)
{
    double price;
    in >> s.bookNo >> s.units_sold >> price;
    // check that the inputs succeeded
    if (in)
        s.revenue = s.units_sold * price;
    else
        s = Sales_item();  // input failed: reset object to default state
    return in;
}

std::ostream&
operator<<(std::ostream& out, const Sales_item& s)
{
    out << s.isbn() << " " << s.units_sold << " "
        << s.revenue << " " << s.avg_price();
    return out;
}

double Sales_item::avg_price() const
{
    if (units_sold)
        return revenue/units_sold;
    else
        return 0;
}
#endif 
Last edited on
What error message do you get?
The errors are:

In file included from main.cpp:2:0:
Sales_item.h:20:20: warning: defaulted and deleted functions only available with -std=c++0x or -std=gnu++0x [enabled by default]
Sales_item.h:34:27: error: ISO C++ forbids initialization of member ‘units_sold’ [-fpermissive]
Sales_item.h:34:27: error: making ‘units_sold’ static [-fpermissive]
Sales_item.h:34:27: error: ISO C++ forbids in-class initialization of non-const static member ‘units_sold’
Sales_item.h:35:22: error: ISO C++ forbids initialization of member ‘revenue’ [-fpermissive]
Sales_item.h:35:22: error: making ‘revenue’ static [-fpermissive]
Sales_item.h:35:22: error: ISO C++ forbids in-class initialization of non-const static member ‘revenue’
You need to turn on C++11 features by passing -std=c++11 to the compiler. If you use an older version try -std=c++0x instead.
Is there anyway i can do that in CodeBlocks..
like in terminal i can type g++ -std=c++11 your_file.cpp -o your_program
but what about CodeBlocks?
Somewhere in the settings. I don't know exactly where but I have seen people giving the explanation a few times so it is probably not hard to find if you search for it.
Last edited on
ok ill search Google..And thank you so much

Edit ..it dosnt show the c++11 in the compilers plugin tab(codeblocks) so I just switched to windows 8 back
Last edited on
code::blocks -> settings -> compiler... -> compiler settings -> other options -> ok
i get error
cc1plus error: unrecognized command line option `-std=c++11

and when i change it to std=c++11 i get an error
g++ error: std=c++11: No such file or directory

same with std=c++0x
Last edited on
`-std=c++11

Odd, I don't get any errors when I compile any of my old code with
g++ -o fileexe filename.cpp -std=c++11

I'm running Crunchbang(#!).
Last edited on by closed account z6A9GNh0
Whats ur version of GCC do this command to check g++ --version

mine is 4.6.3..And do u use COde:Blocks...

How did u install ur G++..can u send me a site,,thanks

edit: i changed to 4.8.1..let me see if it works

Edit again: YES IT WORKS..THANKS ALL SO MUCH...It worked...i just needed to update with a few commands
Last edited on
Glad to hear it. I've been using linux distros for years now so I know the commands already :P. As for my compiler version it is 4.7.2 and I use several editors (Geany, Qt Creator, Code::Blocks, for C++ and others for other languages).
Last edited on by closed account z6A9GNh0
Cool ill check them out...I've only started like I had ubuntu a week ago but I started using it 2day...but why do not many ppl program with linux
CaptainBlastXD wrote:
but why do not many ppl program with linux

Many people do program with linux. They just released a steam client for linux and slowly porting steam games to linux ports. Almost all open source projects are people that use linux distros.

What makes you think there are no linux programmers? I've been doing this for 17 years and have been using a linux distro of some sort for 12 years now. I know quite a few linux programmers at a few sites.
Yea I heard about that and ouya (something like that) that is android on tv.....but I mean on this forum..sry I forgot to specify...everyone here is windows
Oh well the answer to that is because Ubuntu users use the Ubuntu forums to ask programming questions and same for other linux questions. Linux is a programmer's operating system and has been considered this for years. It is only recently that distros like ubuntu have started trying to get a user friendly interface in order to attract non-programmers into linux.

Not sure if the same is said for Windows. I know there are XNA and Unity forums, but I don't know if Microsoft has a Windows specific forum for programming. Though there are only 3 pages difference between the two.

Depends on how you take that though. You can take it as Linux is so easy to develop for that it doesn't need tons of questions or well documented. Or...Well, okay one way to look at it, Windows API and libs are so confusing that people have to ask tons of questions to figure out what they are doing. ;)
BHXSpecter wrote:
It is only recently that distros like ubuntu have started trying to get a user friendly interface in order to attract non-programmers into linux.
I don't know about recently, Ubuntu has been around almost ten years and before that there was Lindows. And quite frankly most distros (and desktop environments) try to make things easier for end users, not just programmers.

Not sure if the same is said for Windows. I know there are XNA and Unity forums, but I don't know if Microsoft has a Windows specific forum for programming. Though there are only 3 pages difference between the two.
Haven't heard of MSDN?
naraku9333 wrote:
Haven't heard of MSDN?

Heard of it, but I wouldn't call it as useful as a forum to get help. Last time I tried using MSDN (when I had Vista six years ago) every time I searched it for what I needed I found nothing that helped. That is one of many reasons I changed to linux systems. I actually started my game programming on windows trying to learn Win32API and DX, quickly went to Allegro and once I realized I didn't need to learn OS specific API I went to linux for good and been using linux only for deving.
I know there are forums. I used them a total of three times, asked three different questions and each got about 100 views and 0 replies. Looking through it now makes me think it hasn't changed. Seeing quite a few questions with 100s of views and only a few replies (like one is 93 views and 1 reply). Appears my account is no longer there either as I am not finding my 3 questions anymore and can't log in. Tells me my email and password are wrong. Oh well, no point in worry about it, don't do any development things under Windows to need it.
Pages: 12