我正在学习面向对象编程的概念。
从动态内存分配开始,并在此代码中发现一个问题。我在这段代码中找不到问题所在,在我看来一切都很好
#include <iostream>
using namespace std;
class Team
{
private:
char *name;
char stadium[20];
char city[20];
public:
Team(const char *i=" ",const char *stadium=" ",const char *city=" ")
{
name=new char [strlen(i)];
strcpy(name,i);
strcpy(this->stadium,stadium);
strcpy(this->city,city);
}
const char *getName() {
return name;
}
const char *getCity() {
return city;
}
const char *getStadium() {
return stadium;
}
void setName(char *name) {
strcpy(this->name, name);
}
~Team() {
delite [] name;
}
};
};
int main()
{
Team *e1 = new Team("Real Madrid", "Madrid", "Santiago Bernabeu");
Team *e2 = new Team(*e1);
cout << e1->getName();
cout << "-";
cout << e2->getName();
e1->setName("Barselona");
cout << e1->getName();
cout << "-";
cout << e2->getName();
delete e1;
delete e2;
return 0;
}我花了3个多小时来解决这个问题...没有找到任何东西,我知道这可能不是我需要寻找解决方案的方式。但我厌倦了尝试解决这个问题。
我得到的一些错误
main.cpp: In constructor ‘Team::Team(const char*, const char*, const char*)’:
main.cpp:13:24: error: ‘strlen’ was not declared in this scope
name=new char [strlen(*i)];
^~~~~~
main.cpp:13:24: note: suggested alternative: ‘mbrlen’
name=new char [strlen(*i)];
^~~~~~
mbrlen
main.cpp:14:9: error: ‘strcpy’ was not declared in this scope
strcpy(name,i);
^~~~~~
main.cpp:14:9: note: suggested alternative: ‘strtoq’
strcpy(name,i);
^~~~~~
strtoq我试过这段代码
#include<iostream>
#include<cstring>
using namespace std;
class Team {
private:
char *name;
char city[20];
char stadion[30];
public:
Team(char *name = "", char *city = "", char *stadion = "") {
this->name = new char[20];
strcpy(this->name, name);
strcpy(this->city, city);
strcpy(this->stadion, stadion);
}
Team(const Team &e) {
strcpy(name, e.name);
strcpy(city, e.city);
strcpy(stadion, e.stadion);
}
const char *getName() {
return name;
}
const char *getCity() {
return city;
}
const char *getStadion() {
return stadion;
}
void setName(char *name) {
strcpy(this->name, name);
}
~Team() {
// delite [] name;
}
};
int main() {
Team *e1 = new Team("Real Madrid", "Madrid", "Santiago Bernabeu");
Team *e2 = new Team(*e1);
cout << e1->getName();
cout << "-";
cout << e2->getName();
e1->setName("Barselona");
cout << e1->getName();
cout << "-";
cout << e2->getName();
delete e1;
delete e2;
return 0;
}并得到这个错误
main.cpp:44:26: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
Segmentation fault (core dumped)发布于 2020-09-05 23:21:42
我更正了你的代码中的一些拼写错误,修改了class Team的拷贝构造函数,它在我的PC上运行良好。
class Team
{
private:
char* name;
char stadium[20];
char city[20];
public:
Team(const char* i = " ", const char* stadium = " ", const char* city = " ")
{
name = new char[strlen(i) + 1]; // +1 for null-terminate
strcpy(name, i);
strcpy(this->stadium, stadium);
strcpy(this->city, city);
}
// allocating memory for Team::name needed, but I delegated it to above constructor.
Team(const Team& e) :Team(e.name, e.stadium, e.city) {
// empty
}
const char* getName() {
return name;
}
const char* getCity() {
return city;
}
const char* getStadium() {
return stadium;
}
void setName(const char* name) { // must be const char*. Not char*!!
strcpy(this->name, name);
}
~Team() noexcept {
if(name)
delete[] name; // delete[] name, NOT delite
}
};下面是编写C++类的更好的方法。正如一些评论所说,比起char*,更喜欢使用std::string。我知道可能很难习惯C++类,但相信我,std::string比char*好得多。使用std::string,您不需要自己分配内存,也不需要编写额外的复制构造函数和析构函数。
#include <string>
using std::string;
class Team
{
public:
Team(void) = default;
explicit Team(const string& name, const string& stadium, const string& city)
:name_(name), stadium_(stadium), city_(city) {}
void setName(const string& new_name) { name_ = new_name; }
string getName(void) const { return name_; }
string getCity(void) const { return city_; }
string getStadium(void) const { return stadium_; }
private:
string name_;
string stadium_;
string city_;
};发布于 2020-11-10 21:04:52
在主文件中添加#include <cstring>:C++
#include <iostream>
using namespace std;
#include<cstring> // Add this in your code在您的主文件中:
int main() {
...
}https://stackoverflow.com/questions/63755145
复制相似问题