I am writing a program where I need to use copy constructor. Since I am novice in using copy constructor I do not know whether my declaration and using of copy constructor is valid or not?
Also here I am facing problem with the display function, the error is ::
error: prototype for 'int Student::display_student()' does not match any in class 'Student'. What is this error?
#include <string>
#include <iostream>
using namespace std;
class Student
{
private:
int rollno;
string name;
public:
Student();
Student(int x, string str);
Student(Student &s);
void display_student();
};
Student::Student()
{
rollno = 0 ;
name = "" ;
}
Student::Student(int x, string str)
{
rollno=x ;
name=str ;
}
Student::Student(Student &s)
{
rollno = s.rollno ;
name = s.name;
}
Student::display_student()
{
cout << "Student Name ::" << name << endl << "Student Roll No. ::" << rollno << endl;
}
int main()
{
Student A;
Student B(09,"Jhonny");
Student C(B);
A.display_student();
B.display_student();
C.display_student();
return 0;
}
You didn't specify the return value in the definition of Student::display_student(). Try:
void Student::display_student()
{
cout << "Student Name ::" << name << endl << "Student Roll No. ::" << rollno << endl;
}
Compiler assumes int Student::display_student() by default. The class declaration contains the prototype for void display_student() but you provided only the definition for function int display_student().
Copy constructor signature uses generally const reference. In your case, you may use the default implementation:
Student(const Student&) = default;
You may also add copy assignment:
Student& operator=(const Student&) = default;
Related
I have a set of a custom class and when I'm trying to insert an object of that class, the terminal gives me an error:
#ifndef EMPLOYEE_HH
#define EMPLOYEE_HH
#include <string>
#include <iostream>
#include <set>
#include <iterator>
using namespace std ;
class Employee {
public:
// Constructor
Employee(const char* name, double salary) : _name(name), _salary(salary) {}
// Accessors
const char* name() const { return _name.c_str() ; }
double salary() const { return _salary ; }
// Print functions
void businessCard(ostream& os = cout) const {
os << " +------------------+ " << endl
<< " | ACME Corporation | " << endl
<< " +------------------+ " << endl
<< " " << name() << endl ;
}
private:
string _name ;
double _salary ;
} ;
class Manager : public Employee{
public:
Manager(const char* name, double salary) : Employee(name, salary) {};
void addSubordinate(Employee& empl){
_subs.insert(empl);
}
const set<Employee*>& listOfSubordinates() const{
return _subs;
}
void businessCard() const{
Employee::businessCard();
set <Employee*>::iterator it=_subs.begin();
cout <<"Managed employees: " <<endl;
while(it!=_subs.end()){
cout <<*it++ <<endl;
}
}
private:
set <Employee*> _subs;
};
#endif
The addSubordinate() routine:
void addSubordinate(Employee& empl){
_subs.insert(empl);
}
returns this error:
no instance of overloaded function "std::set<_Key, _Compare, _Alloc>::insert [with _Key=Employee *, _Compare=std::less<Employee *>, _Alloc=std::allocator<Employee *>]" matches the argument list
I have tried to overload the operator < as other people suggested in response to similar questions, but that doesn't seem to solve the issue.
Your set accepts a pointer to an Employee, but you are trying to insert the object itself.
What you can do is
void addSubordinate(Employee& empl){
_subs.insert(&empl); // This will store the address to the object
}
or accept a pointer itself
void addSubordinate(Employee* empl){
_subs.insert(empl);
}
You need to define the < operator in the Employee class. The items in a set are always sorted and since you don't have a comparator it won't let you insert an instance of that class.
Try something like this:
bool operator<(const Employee& emp){
return salary() < emp.salary();
}
I've been working on this assignment for a while. Here's the instructions:
You are to design an abstract class called Employee whose members are
as given below (make them protected):
Data members: char *name, long int ID
Two constructors: A Default constructor // intitialize data memebrs to
the default values and a copy constructor
Methods: setPerson (char *n, long int id) //allows user to set
information for each person A function called Print () // should be a
virtual function, that prints the data attributes of the class. and a
destructor
Also define two classes that derived from class Employee, called
Manager and Secretary. Each class should inherit all members from the
base class and has its own data members and member functions as well.
The Manager should have a data member called degree for his/her
undergraduate degree (e.g. diploma, bachelor, master, doctor), the
Secretary should have her contract (can be a Boolean value 1/0 for
permanent/temporary).
All member functions of derived class should be overrided from their
base class.
Write the following main() to test your classes
int main() {
Employee * p = new Manager(“Bruce Lee”, 0234567, “Dr.”);
P.print();
Secretary p2;
p2.setPerson(“Wilma Jones”, 0341256, “permanent”);
delete p;
p = & p2;
p.Print();
return 0;
}
This is everything I've come up with so far, but I'm pretty sure it's riddled with mistakes and that my arguments and variable types are all off.
#include <iostream>
using namespace std;
class Employee{
protected:
char *name;
long int ID;
public:
Employee();
Employee(Employee&);
void setPerson(char * n, long int eID) {
name = n;
ID = eID; };
virtual void Print(){
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl; };
};
class Manager: public Employee {
protected:
char *degree;
public:
void setPerson(char * n, long int eID, char * d){
name = n;
ID = eID;
degree = d;
};
void Print() {
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
cout << "Degree: " << degree << endl;
};
};
class Secretary: public Employee {
protected:
bool contract;
public:
void setPerson(char * n, long int eID, string c){
name = n;
ID = eID;
if (c == "permanent") contract = true;
else contract = false;
};
void Print(){
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
cout << "Contract: " << contract << endl;
};
};
int main() {
Employee * P = new Manager("Bruce Lee", 0234567, "Dr.");
P.Print();
Secretary P2;
P2.setPerson("Wilma Jones", 0341256, "permanent");
delete P;
P = & P2;
P.Print();
return 0;
}
I'm getting an error on line 62 (the first line of the main code):
No matching constructor for initialization of Manager
I've tried reading similar questions, but they haven't helped me much. I think the most confusing thing is contract being a bool value and the use of char arguments. Any guidance at all is appreciated.
The error you're getting is pretty straight-forward: you don't have any constructor for Manager (or Employee) that takes a string, integer (?), and string as arguments.
You have declared the constructor employee but not defined it.
Look at the class employee, Under publc you have declared
Employee();
Employee(Employee&);
But you have not defined the function. You need
Employee :: Employee()
{
bla bla bla
}
and another one for the other signature.
CustomerInfo.cpp
#include "CustomerInfo.h" // <==== Funtion Definition
CustomerInfo::CustomerInfo() // <==== The Scope Resolution which is two colons :: gives me access to the class
{
newZipCode = 0;
}
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
{
newName = name;
newAddress = address;
newZipCode = zipCode;
}
CustomerInfo::~CustomerInfo()
{
}
string CustomerInfo::getName() const
{
return newName;
}
string CustomerInfo::getAddress() const
{
return newAddress;
}
int CustomerInfo::getZipCode() const
{
return newZipCode;
}
The main.cpp file
#include <iostream>
#include <string>
#include "CustomerInfo.h"
using namespace std;
int main()
{
string name;
string address;
int zipCode;
cout << "Enter your name: ";
getline (cin, name);
cout << "Enter your address" << endl;
getline (cin, address);
cout << "Enter your ZipCode: ";
cin >> zipCode;
CustomerInfo Real_1(name, address, zipCode);
cout << endl << " Name: " << Real_1.getName() << endl <<
"Address: " << Real_1.getAddress() << endl <<
"ZipCode: " << Real_1.getZipCode() << endl;
return 0;
}
The CustomerInfo.h file
#ifndef CUSTOMERINFO_H
#define CUSTOMERINFO_H
// Header ==> Function Declaration
#include <iostream>
#include <string>
using namespace std;
class CustomerInfo
{
public:
CustomerInfo(); // <==== Default Constructor
CustomerInfo(string, int); // <==== Overload Constructor
~CustomerInfo(); // <===== Destructor - Done using an object it will be destroyed out of memory'
string getName() const; // <==== Accessor Functions - Return member variables one value at a time. In addition, no void function will be used.
// getName - returns name of person
string getAddress() const;
// getAddress - returns address of person
int getZipCode() const;
// getZipCode - returns zipcode of person
private:
//Member Variables
string newName;
string newAddress;
int newZipCode;
}; // <=== Requires semicolon after brackets for classes
#endif // CUSTOMERINFO_H
The error I receive is out-of-line definition of 'CustomerInfo' does not match any declaration in 'CustomerInfo'
The following line is the error
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
Your problem seems to be that you define a constructor
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
which does not appear in the class definition (in CustomerInfo.h). The closest one is
CustomerInfo(string, int);
but it doesn't match the signature. Just replace it with
CustomerInfo(string name, string address, int zipCode);
to your class definition in the header file.
This question already has answers here:
extra qualification error in C++
(5 answers)
Closed 7 years ago.
This is a program from a free c++ ebook, but it won't compile.
At first I typed it out, but upon realizing that it wouldn't compile I tried copying out of the ebook directly into codeblocks 13.12.
It still won't compile. The ebook is from 2010 so maybe the code doesn't follow current standards or there is a syntax typo somewhere.
Please help me figure out what is wrong.
The error is:
error: extra qualification 'Critter::' on member 'operator=' [-fpermissive]|
The code is:
#include <iostream>
using namespace std;
class Critter
{
public:
Critter(const string& name = "", int age = 0);
~Critter(); //destructor prototype
Critter(const Critter& c); //copy constructor prototype
Critter& Critter::operator=(const Critter& c);
op
void Greet() const;
private:
string* m_pName;
int m_Age;
};
Critter::Critter(const string& name, int age)
{
cout << "Constructor called\n";
m_pName = new string(name);
m_Age = age;
}
Critter::~Critter() //destructor definition
{
cout << "Destructor called\n";
delete m_pName;
}
Critter::Critter(const Critter& c) //copy constructor definition
{
cout << "Copy Constructor called\n";
m_pName = new string(*(c.m_pName));
m_Age = c.m_Age;
}
Critter& Critter::operator=(const Critter& c)
{
cout << "Overloaded Assignment Operator called\n";
if (this != &c)
{
delete m_pName;
m_pName = new string(*(c.m_pName));
m_Age = c.m_Age;
}
return *this;
}
void Critter::Greet() const
{
cout << "I’m " << *m_pName << " and I’m " << m_Age << " years old.\n";
cout << "&m_pName: " << cout << &m_pName << endl;
}
void testDestructor();
void testCopyConstructor(Critter aCopy);
void testAssignmentOp();
int main()
{
testDestructor();
cout << endl;
Critter crit("Poochie", 5);
crit.Greet();
testCopyConstructor(crit);
crit.Greet();
cout << endl;
testAssignmentOp();
return 0;
}
void testDestructor()
{
Critter toDestroy("Rover", 3);
toDestroy.Greet();
}
void testCopyConstructor(Critter aCopy)
{
aCopy.Greet();
}
void testAssignmentOp()
{
Critter crit1("crit1", 7);
Critter crit2("crit2", 9);
crit1 = crit2;
crit1.Greet();
crit2.Greet();
cout << endl;
Critter crit3("crit", 11);
crit3 = crit3;
crit3.Greet();
}
Like the message says, there is an extra Critter:: in
Critter& Critter::operator=(const Critter& c);
As this is inside the class declaration, it must be a member of the class and doesn't need to be prefixed with the class name.
Critter& operator=(const Critter& c);
is the correct form.
The class name prefix is used only when the members are defined outside the class, like in the code later in the example.
remove the Critter:: from that operator prototype
noob here. Having a doubt while doing an exercise from a book.
The doubt is the following: Why if I use a const std::string * as an argument to a method, the compiler sends me the following error: no matching function for call to 'BankAccount::define(const char [11], const char [11], int)' in the client?
If I swap the prototype and definition of the method to accept as an argument a const std::string & (like I did in the constructor) it's no problem for the compiler.
Client:
// usebacnt.cpp --
#include "BankAccount.h"
#include <iostream>
int main()
{
BankAccount john;
BankAccount mary("Mary Wilkinson", "5000000000"); // balance set to 0 because of default argument
john.display();
mary.display();
john.define("John Smith", "4000000000", 26); // error line
mary.deposit(1000.50);
john.withdraw(25);
john.display();
mary.display();
std::cin.get();
return 0;
}
Class declaration:
// BankAccount.h -- Header file for project Exercise 10.1
#ifndef BANKACCOUNT_H_
#define BANKACCOUNT_H_
#include <string>
class BankAccount
{
private:
std::string fullName;
std::string accountNumber;
double balance;
public:
BankAccount(); // default constructor
BankAccount(const std::string &fN, const std::string &aN, double b = 0.0); // constructor with a default argument
void display() const;
void deposit(double amount);
void withdraw(double amount);
void define(const std::string *pfN, const std::string *paN, double b);
};
#endif
Class implementation:
// methods.cpp -- Compile alongside usebacnt.cpp
#include "BankAccount.h"
#include <iostream>
void BankAccount::display() const
{
using std::cout;
using std::ios_base;
ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield); // formatting output and saving original
cout.precision(2);
cout << "Full Name: " << fullName << '\n';
cout << "Account Number: " << accountNumber << '\n';
cout << "Balance: " << balance << "\n\n";
cout.setf(orig);
}
void BankAccount::deposit(double amount)
{
if (amount < 0)
{
std::cout << "You can't deposit a negative number! Amount set to zero.";
amount = 0;
}
balance += amount;
}
void BankAccount::withdraw(double amount)
{
if (amount < 0)
{
std::cout << "You can't withdraw a negative number! Amount set to zero.";
amount = 0;
}
if (balance < amount)
{
std::cout << "You can't withdraw more money than you have. Amount set to zero.";
amount = 0;
}
balance -= amount;
}
void BankAccount::define(const std::string *pfN, const std::string *paN, double b)
{
fullName = *fN;
accountNumber = *aN;
balance = b;
}
// constructors
BankAccount::BankAccount() // default constructor
{
fullName = "empty";
accountNumber = "empty";
balance = 0.0;
}
BankAccount::BankAccount(const std::string &fN, const std::string &aN, double b) // constructor with default argument
{
fullName = fN;
accountNumber = aN;
balance = b;
}
Shouldn't the compiler interpret the literal as a string object (like it does with the reference)? So if I say it's a pointer it should pass the address of the string (its first element).
Any help?
Shouldn't the compiler interpret the literal as a string object (like it does with the reference)?
No. Pointers are not references; smack anyone who tells you that they are.
It all comes down to this: std::string is not a string literal. Therefore, when you pass a string literal to a function that takes a std::string, a temporary std::string that holds the contents of the literal must be created. This is done with the conversion constructor of std::string.
A const& is allowed to be initialized from a temporary, which allows conversion constructors to work their magic. A const* must be a pointer, and you're not supposed to get pointers to temporaries. Therefore, const* cannot be magically filled in by a temporary created from a conversion constructor.
You should use a const std::string & instead of a pointer. Or a std::string value if you have C++11 move constructors available.