Copy constructor error c2039 - c++

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!

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) {}
^^^^^^^^^^^^^

You probably meant
Comanda::Comanda(const string &nume)
{
_nume = nume;
}
It's not a copy constructor by the way.

_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.

Related

assignment operator error in unique pointer

I tried to use unique_ptr in c++ in a singelton pattern instead of raw pointer. when I want to assign a unique_ptr to another I got an error. I tried to use std::move to assign but it did not work. the code is as follow:
#include <iostream>
#include <memory>
#include <list>
#include <algorithm>
#include <iterator>
#include <string>
using namespace std;
class ClientDB
{
private:
static unique_ptr<ClientDB> theDB;
ClientDB() {}
list<string> clients;
public:
~ClientDB() {}
static unique_ptr<ClientDB> getInstance()
{
if(theDB==nullptr)
theDB = make_unique<ClientDB>;
return theDB;
}
void addClient(string c) {clients.push_back(c);}
void printClients(ostream& os)
{
copy(clients.cbegin(),clients.cend(),ostream_iterator<string>{os,"\n"});
}
};
int main()
{
unique_ptr<ClientDB> db1{ClientDB::getInstance()};
db1->addClient("Mr. Schultz");
unique_ptr<ClientDB> db2{ClientDB::getInstance()};
db2->addClient("Mrs. James");
unique_ptr<ClientDB> db3{ClientDB::getInstance()};
db3->addClient("Mr. Karajan");
db1->printClients(cout);
}
and the error I got is
error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<ClientDB>’ and ‘<unresolved overloaded function type>’)
theDB = make_unique<ClientDB>;
and another question is if nullptr can be used for unique_ptr.
Finally by help of my teacher, I can solve the problem. there is some points should be considered.
1- for unique_ptr assignment, std::move:: should be used.
2- make_unique has no access to the private constructor, the constructor should be called explicitly:
theDB = unique_ptr<ClientDB>(new ClientDB());
3-The unique-ptr must be initialized outside the class:
unique_ptr<ClientDB> ClientDB::theDB;
4- Three unique pointers in main for the same object can not be used, only one is allowed for uniqueness. references to unique_ptr should be used instead:
unique_ptr<ClientDB>& db1=ClientDB::getInstance();
and finally the code should be like this
#include <iostream>
#include <memory>
#include <list>
#include <algorithm>
#include <iterator>
#include <string>
using namespace std;
class ClientDB
{
private:
static unique_ptr<ClientDB> theDB;
ClientDB() {}
list<string> clients;
public:
~ClientDB() {}
static unique_ptr<ClientDB>& getInstance()
{
if(theDB==nullptr)
//theDB = move(make_unique<ClientDB>());
theDB = unique_ptr<ClientDB>(new ClientDB());
return theDB;
}
void addClient(string c) {clients.push_back(c);}
void printClients(ostream& os)
{
copy(clients.cbegin(),clients.cend(),ostream_iterator<string>{os,"\n"});
}
};
unique_ptr<ClientDB> ClientDB::theDB;
int main()
{
unique_ptr<ClientDB>& db1=ClientDB::getInstance();
db1->addClient("Mr. Schultz");
unique_ptr<ClientDB>& db2=ClientDB::getInstance();
db2->addClient("Mrs. James");
unique_ptr<ClientDB>& db3=ClientDB::getInstance();
db3->addClient("Mr. Karajan");
db1->printClients(cout);
}

Member variable in scope of one member function but not the other

I'm seriously confused why this is happening. I get an error 'enzyme_acronyms_ was not declared in this scope'. It points to my writeAcronym function but not getAcronym, and both use enzyme_acronyms_. What can possibly cause this?
SequenceMap.h
#ifndef SequenceMap_h
#define SequenceMap_h
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class SequenceMap
{
private:
string recognition_sequence_;
vector<string> enzyme_acronyms_;
public:
string getAcronym();
void writeAcronym(string an_enz_acro);
}
SequenceMap.cpp
#include "SequenceMap.h"
string SequenceMap::getAcronym()
{
return enzyme_acronyms_[0]; //works fine
}
void writeAcronym(string an_enz_acro)
{
enzyme_acronyms_.push_back(an_enz_acro); //enzyme_acronyms_ not declared in this scope
}
You've missed the SequenceMap:: qualification on the second function definition:
void SequenceMap::writeAcronym(string an_enz_acro)
It must be declared like this:
void SequenceMap::writeAcronym(string an_enz_acro)
{
enzyme_acronyms_.push_back(an_enz_acro);
}
You forgot the class scope SequenceMap::.

What's wrong - expected class-name before '{' token

What's wrong in this code ? I get [Error] expected class-name before '{' token (Pralka.h line 14 )
i know there are lots of similar questions here. I've googled too but I can't get over it. So I would like to show you my code..
I wrote this very simple code to train myself on inheritance and virtual functions..
main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include "AGD.h"
using namespace std;
int main() {
Pralka p1("polar", 1250);
AGD *A;
A = &p1;
}
AGD.h:
#ifndef AGD_H
#define AGD_H
#include <iostream>
#include "Pralka.h"
class AGD {
private:
static int liczba_sprzetow;
public:
AGD(){
liczba_sprzetow++;
}
~AGD(){
liczba_sprzetow--;
}
static int get_liczba_sprzetow() {
return liczba_sprzetow;
}
virtual double get_cena() {
}
};
#endif
Pralka.h:
#ifndef PRALKA_H
#define PRALKA_H
#include <iostream>
#include <string>
using namespace std;
class Pralka : public AGD
{
private:
string marka;
double cena;
public:
Pralka(string m, double c): marka(m), cena(c){
}
Pralka(){
}
~Pralka(){
}
string get_marka() const{
return marka;
}
double get_cena() const{
return cena;
}
Pralka& operator=(const Pralka& Q){
marka=Q.marka;
cena=Q.cena;
}
};
#endif
I get also [Error] cannot convert 'Pralka*' to 'AGD*' in assignment but why? I don't understand (main.cpp line 29).
AGD.h is including Pralka.h, but it should be the other way round (Pralka.h should be including AGD.h).
The reason is that Pralka needs to see the AGD declaration to inherit from it. AGD doesn't need to see the Pralka declaration.

syntax error "does not name a type"

Why does my following code produce the following while compiling: error: 'Individual' in class 'Evolve' does not name a type.
#ifndef EVOLVE_H
#define EVOLVE_H
#include <cstdlib>
#include <iostream>
#include <string>
#include "Operator.h"
#include "Individual.h"
using namespace std;
class Evolve
{
public:
Evolve(int length, Operator** operators, int numOperators);
Individual* bestIndividual;
Individual* run(int generations);
Operator operatorArray[];
private:
int length;
int numOperators;
};
#endif
And my class file is
#include <cstdlib>
#include <iostream>
#include <string>
#include "Evolve.h"
#include "Operator.h"
#include "Individual.h"
using namespace std;
Evolve::Evolve(int length, Operator** operators, int numOperators)
{
Individual* bestIndividual = new Individual(length);
}
Evolve::Individual* run(int generations)
{
for(int i=0; i<generations; i++)
{
cout << "test counter = " << i << endl;
}
}
I've read a few other posts about the error and it has all been about what order to declare the function, but I'm not sure if thats the cause of my problem.
The way you implement member function is incorrect.
Update:
Evolve::Individual* run(int generations)
to:
Individual* Evolve::run(int generations)
Also, to initialize member you do not re-define it again.
Evolve::Evolve(int length, Operator** operators, int numOperators)
: bestIndividual(new Individual(length)
{
}
In your constructor,
Individual* bestIndividual = new Individual(length);
you defined a local pointer bestIndividual and it leaks memory.
The general syntax is:
return-type class-name :: function-name(arg list)
Individual* Evolve :: run (int generations)
Fix your Evolve definition

C++ - Declaration of method incompatible

EDIT:
I'm using namespace std
I'm using VS10
Room is a separate class
I've included the memory header in all necessary files
The original error was an Intellisense error I was getting before building. After building, I got a buttload more:
[The original Intellisense error before building] declaration is incompatible with "std::tr1::shared_ptr<< error-type >> Option::getRoom()
'std::tr1::shared_ptr<_Ty> Option::getRoom(void)' : overloaded function differs only by return type from 'std::tr1::shared_ptr Option::getRoom(void)'
'Option::getRoom' : redefinition; different basic types
'Option::getRoom' uses undefined class 'std::tr1::shared_ptr'
These are related to this piece of code in Option.cpp:
shared_ptr<Room> Option::getRoom(){
shared_ptr<Room> room(new Room);
return room;
}
The corresponding code in Option.hpp:
public:
virtual shared_ptr<Room> getRoom();
Error 'RoomOption::getRoom': overriding virtual function return type differs and is not covariant from 'Option::getRoom'
[IntelliSense] return type is not identical to nor covariant with return type "std::tr1::shared_ptr<< error-type >>" of overridden virtual function function "Option::getRoom"
This is related to this piece of code in RoomOption.hpp, a subclass of Option:
public:
shared_ptr<Room> getRoom();
Here's all the code from the two classes I'm having trouble with:
Option.h:
#pragma once
#include "Room.h"
#include <memory>
using namespace std;
class Option
{
protected:
int id;
char* text;
public:
Option(void);
Option(int, char*);
virtual ~Option(void);
char* getText();
int getID();
virtual shared_ptr<Room> getRoom();
};
Option.cpp:
#include "Option.h"
#include "Room.h"
#include <memory>
using namespace std;
Option::Option(void)
{
}
Option::Option(int newID, char* newText){
id = newID;
text = newText;
}
Option::~Option(void)
{
}
char* Option::getText(){
return text;
}
int Option::getID(){
return id;
}
shared_ptr<Room> Option::getRoom(){
shared_ptr<Room> room(new Room());
return room;
//note that this function will never be used. I'd prefer to
//pass back a null pointer but I couldn't do that either.
}
RoomOption.h:
#pragma once
#include "Option.h"
#include "Room.h"
#include <memory>
using namespace std;
class RoomOption :
public Option
{
private:
shared_ptr<Room> room;
public:
RoomOption(void);
RoomOption(int, char*, shared_ptr<Room>);
~RoomOption(void);
void setRoom(shared_ptr<Room>);
shared_ptr<Room> getRoom();
};
RoomOption.cpp:
#include "RoomOption.h"
#include "Room.h"
#include <memory>
using namespace std;
RoomOption::RoomOption(void)
{
}
RoomOption::RoomOption(int newID, char* newText, shared_ptr<Room> newRoom)
{
id = newID;
strcpy(text, newText);
room = newRoom;
}
RoomOption::~RoomOption(void)
{
}
void RoomOption::setRoom(shared_ptr<Room> newRoom){
room = newRoom;
}
shared_ptr<Room> RoomOption::getRoom(){
return room;
}
This code compiles without error at /W4 /WX with VS 2010:
#include <memory>
struct Room {};
class Option {
public:
std::shared_ptr<Room> getRoom();
};
std::shared_ptr<Room> Option::getRoom(){
std::shared_ptr<Room> room(new Room());
return room;
}
int main() {
Option opt;
std::shared_ptr<Room> room = opt.getRoom();
return 0;
}
What are you doing differently?
Is Room declared at the point where it's used in the getRoom() call in Option.hpp?
Have you tried removing the () from new Room() in case you're somehow getting hit by the most vexing parse, possibly in other code we can't see??