1

hy guys! I have a code that gives me headaches. I would like some help please. This is my .h file.

#include <iostream>
#include <string>
using namespace std;

namespace UI{
class Comanda
{
private:
    const string _nume;
public:
    Comanda();
    Comanda(const string &nume);

    virtual ~Comanda();
    const string& Nume() const;
    virtual void AsteaptaEnter();
    virtual void Execute();
};
};

And the .cpp:

#include <iostream>
#include <string>
#include "Comanda.h"
#include "Exceptii.h"

using namespace std;
using namespace UI;

Comanda::Comanda()
{
    cout << "Comanda()" << endl;
}

Comanda::Comanda(const string &nume)
{
    _nume = nume._nume;
}

The compiler shows me this error:

error C2039: '_nume' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'

What should i do? Thanks in advance!

  • Note that `using namespace std;` in a header is [considered evil](http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers). – Cornstalks Nov 26 '13 at 20:21
  • @jdc : tried that too...and then it shows me this: error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) – user3033831 Nov 26 '13 at 20:22

3 Answers3

1

You must initialize the constant members in the initializer list of ctor, and also nume._nume is not valid.

Comanda::Comanda(const string &nume) : _nume(nume) {}
                                     ^^^^^^^^^^^^^
masoud
  • 55,379
  • 16
  • 141
  • 208
  • @user3033831: Actually here is night ;-) and it the answer helped you, you can _accept_ it by clicking on the green tick at left side of this answer. – masoud Nov 26 '13 at 20:56
0

You probably meant

Comanda::Comanda(const string &nume)
{
    _nume = nume;
}

It's not a copy constructor by the way.

jdc
  • 319
  • 2
  • 5
0

_nume = nume._nume; this is wrong

it should be _nume = nume;

And as correctly pointed out by @Cornstalks you cannot achieve above assignments in any case as _nume is const.

Nik
  • 1,294
  • 10
  • 16