class MyString:public string
{
public:
MyString(){ string();}
MyString(const char* name){
string(name);
}
MyString(const MyString& a){
*this = a;
}
MyString(const string& a):string(a){}
MyString operator()(int start,int end){
MyString ret(substr(start,end));
return ret;
}
};
when I write this, it shows that
‘const char* name’ previously declared here
10 | MyString(const char* name){
| ~~~~~~~~~~~~^~~~
and string(name);
|
what should I do?
just like words written above
If you really want to write your own string class based in the standard string class then the way to do it is to use composition not inheritance. Based on the code written above, something like this
class MyString
{
public:
MyString() {}
MyString(const char* name) : my_string(name) {}
MyString(const std::string& name) : my_string(name) {}
MyString operator()(int start, int end) const {
return my_string.substr(start, end);
}
private:
std::string my_string;
};
Related
I can compile normal,when I use vector:
TEST(function_obj,bindMemeber1){
std::vector<Person> v {234,234,1241,1241,213,124,152,421};
std::for_each(v.begin(),v.end(), std::bind(&Person::print,std::placeholders::_1) );
}
but when I use set,something wrong:
TEST(function_obj,bindMemeber1){
std::set<Person,PersonCriterion> v{234,234,1241,1241,213,124,152,421};
std::for_each(v.begin(),v.end(), std::bind(&Person::print,std::placeholders::_1) );
}
clion's tips
The IDE tell me that something wrong.when I force IDE to compile, it also can't compile successfully .
Below is the code of Person;
class Person{
private:
size_t no;
std::string name;
public:
Person():no(0){};
Person(size_t n): no(n){};
Person(const Person& p):no(p.no),name(p.name){};
friend class PersonCriterion;
size_t getNo() const;
void print(){
std::cout<<no<<' ';
}
const std::string &getName() const;
};
class PersonCriterion{
public:
bool operator()(const Person& p1,const Person& p2){
return p1.no<=p2.no;
}
};
size_t Person::getNo() const {
return no;
}
const std::string &Person::getName() const {
return name;
}
Elements got from std::set are const-qualified; they're supposed to be non-modifiable. You should mark Person::print as const then it could be called on a const object.
class Person {
...
void print() const {
// ^^^^^
std::cout<<no<<' ';
}
...
};
BTW: Better to mark operator() in PersonCriterion as const too.
class PersonCriterion {
public:
bool operator()(const Person& p1, const Person& p2) const {
return p1.no<=p2.no;
}
};
this is my first question so please bear with any formatting mistakes i make, I'll try to edit them :)
I've got this function which finds the max of three variables of some data type X and returns it.
template <class X>
X fmax(X a, X b, X c)
{
X temp=a;
if (b>temp)
temp=b;
if(c>temp)
temp=c;
return temp;
}
Then there is the class Person which looks like this
class Person
{
private:
char* name;
int height;
char gender;
public:
Person(){}
Person(char * name,int height, char gender)
{
int sz=strlen(name);
this->name= new char [sz];
strcpy(this->name,name);
this->height=height;
this->gender=gender;
}
void setName(char* name)
{
int sz=strlen(name);
this->name= new char [sz];
strcpy(this->name,name);
}
void setHeight(int h){this->height=h;}
void setGender(char g){this->gender=g;}
char* getName(){return this->name;}
int getHeight(){return this->height;}
char getGender(){return this->gender;}
Person operator= (Person p)
{
int sz=strlen(p.getName());
this->name= new char [sz];
strcpy(this->name,p.getName());
this->height=p.getHeight();
this->gender=p.getGender();
return *this;
}
bool operator> (Person p)
{
if(this->getHeight()>p.getHeight())//The persons should be compared using their heights.
return true;
return false;
}
};
and I also overloaded the ostream:
ostream &operator<<(ostream &mystream, Person &p)
{
mystream<<"The person's name is: "<<p.getName()<<endl;
mystream<<"The person's height is: "<<p.getHeight()<<endl;
mystream<<"The person's gender is: "<<p.getGender()<<endl;
return mystream;
}
But I'm getting the error in my main:
int main()
{
Person a("Zacky",178,'m');
Person b("Jimmy",199,'m');
Person c("Matt",200,'m');
Person d=fmax(a,b,c);
cout<<d<<endl;
cout<<fmax(a,b,c);<<endl;//the error strikes here.
return 0;
}
Apparently I can cout the object d after i've initialised it using the fmax function but can't directly cout what is returned by the function. Any idea about what I need to fix?
P.S. I'm totally sorry if this was asked before, I searched the site and didn't find something similar :/
You need to change the following things:
Since fmax() returns an unchangable rvalue you should use
ostream &operator<<(ostream &mystream, const Person &p);
// ^^^^^
as signature for the output operator overloading (I would recommend doing that in general).
That in turn would require to make your getter functions const member functions:
class Person
{
// ...
const char* getName() const {return this->name;}
int getHeight() const {return this->height;}
char getGender() const {return this->gender;}
};
I defined simple conversion method
operator string() { return /*...*/; }
When I call it directly
obj.operator string()
It works fine, but when I call it this way ...
(string)obj
Result is empty string.
What's going on? (I'm using gcc c++14) (Can post code If needed)
Class
class String : public std::string {
std::string str_;
public:
String() {};
String(const String & s) {
str_ = std::string(s.str_);
};
String(String && s) {
str_ = std::string(s.str_);
};
String(const string & s) {
str_ = std::string(s);
};
String(const char * s) {
str_ = std::string(s);
};
char & operator[](size_t i) {
return str_[i];
};
String & operator=(const String & str) {
if (this != &str) {
str_ = str.str_;
}
return *this;
};
String & operator=(String && str) {
if (this != &str) {
str_ = str.str_;
}
return *this;
};
bool operator==(const String & str) {
return str_ == str.str_;
};
bool operator!=(const String & str) {
return str_ != str.str_;
};
operator string() {
return str_;
};
};
The problem is that your class is deriving from std::string.
Remove the : public std::string part and the conversion operator will be used.
You class is already declaring an std::string member (so it uses the "has-a" approach) and doesn't need the "is-a" approach.
By the way deriving from standard library classes is almost always a bad idea (they were not designed for that).
#include <iostream>
using namespace std;
class DrivingLicence
{
protected:
Person owner;
char * type;
Date validity;
int id;
static int cid;
public:
DrivingLicence(Person &o,char* t,Date &d);
DrivingLicence(Person &o,char* t);
DrivingLicence(const DrivingLicence & other);
Date getValidity() const;
int checkValidity() const;
void print() const;
bool operator== (const DrivingLicence& o) const;
void operator +(const int num);
const DrivingLicence& operator= (const DrivingLicence& other);
~DrivingLicence();
};
class Person
{
private:
int id;
char* name;
Date birthday;
public:
Person(char* name,int id1,Date &d);
Person(const Person &other);
~Person();
Date getBirthday() const;
const Person& operator= (const Person & other);
void print() const;
};
class Date
{
int day;
int month;
int year;
public:
Date (int day,int month,int year);
~Date();
const Date& operator=(const Date& other);
bool operator==(const Date & other);
void print() const;
int getYear()const;
int getMonth()const;
int getDay()const;
};
up above are my classes,
i need to initialize both constructors in the DrivingLicence class (not the copy cons), however i cant manage to do that.
can someone please help me with the syntax for this question??
what i mean is :
#include <NameOfHeaderFile>
DrivingLicense::DrivingLicense( Person &o,char* t,Date &d ) : //Code here for 1stconstructor
{
}
DrivingLicense::DrivingLicense( Person &o,char* t ) ://Code here for 2nd constructor
{
}
i don't know how to initialize the values
I assume this is a header file, however usually there is only 1 class per .h file. So you need to create a file called DrivingLicence.cpp.
In this file write:
#include <NameOfHeaderFile>
DrivingLicense::DrivingLicense( Person &o,char* t,Date &d )
{
//Code here for 1stconstructor
}
DrivingLicense::DrivingLicense( Person &o,char* t )
{
//Code here for 2nd constructor
}
Is this what you are asking???
If I have understood your problem, you want a DriverLicence constructor that
also initializes the person inside the DriverLicense instance.
If so, you only have to add this overload to your class:
DriverLicense::DriverLicense(
// Data for Person
char *person_name, int person_id, Date day_birth,
// Data for Driver licence
char *t, Date d)
{
owner = Person(person_name, person_id, day_birth);
// More code here initializing DeriverLicence members.
}
Remember of course add it to your class:
class DriverLicence
{
public:
// ...
DriverLicense::DriverLicense(char *, int, Date, char *t, Date d);
// ...
}
A couple of Advice:
You're working with C++, use std:string instead char*
Don't abuse of passing arguments as references. Some times this is not
the best to do.
If you're trying to initialize the members of your DrivingLicense class with the arguments passed to the constructor, you can do that in your initializer list.
It's would be wise to put these definitions in a cpp. Also note the changes to types and constness or the arguments I'm providing.
DrivingLicense::DrivingLicense(const Person &o, const std::string& t, const Date &d )
: owner(o)
, type(t)
, validity(d)
{ }
DrivingLicense::DrivingLicense(const Person &o, const std::string& t)
: owner(o)
, type(t)
{ } // Note that 'validity' here is default constructed.
include "header.h"
int DrivingLicence::cid =1000;
DrivingLicence::DrivingLicence(Person &o, char* t, Date &d ): owner(o), type(t), validity(d)
{
}
DrivingLicence::DrivingLicence(Person &o,char* t) :owner(o), type(t),validity(8,10,2024){}
this is what i meant.
thanks for all the answers.
I was implementing a String and was giving the definition in .h file. The code in String.h is the following:
#include<list>
class String
{
public:
String();//Constructor
String(char * copy);//For converting CString to String
const char *c_str(const String ©);//For converting String to Cstring
String(list<char> ©);//Copying chars from list
//Safety members
~String();
String(const String ©);
void operator = (const String ©);
protected:
int length;
char *entries;
};
The error is mentioned in the subject. What is it that I am not following?
You are missing a std:: in front of list<char> :
String(std::list<char> ©);
Fixed several of your issues at once:
#include <list>
class String
{
public:
String();
String(const String &c);
String(const char * c);
String(std::list<char> c); // No idea why someone would have this constructor, but it was included in the original ...
~String();
String& operator = (const String &c);
const char *c_str();
private:
unsigned int length;
char* entries;
};