将具有字符串成员的对象添加到向量

发布于 2024-12-24 03:14:46 字数 247 浏览 1 评论 0原文

class A
{
public:
    string  name;

    float   length;

    float   weight;

    bool    isEnabled;
};

当我对此类的向量执行 push_back() 时,它第一次起作用,但在后续的 push_back() 调用中不起作用。难道是因为字符串成员的原因?如果是这样,为什么?

class A
{
public:
    string  name;

    float   length;

    float   weight;

    bool    isEnabled;
};

When I'm doing push_back() on the vector of this class, it works for the first time, but doesn't on the subsequent push_back() calls. Could it be because of the string member? If so, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我乃一代侩神 2024-12-31 03:14:46

我尝试了你的代码。

vector<Aclass>  aClass;
aClass.push_back(Aclass("John Doe ", 6.2 , 220  , true) );
aClass.push_back(Aclass("John Doe2 ", 6.2 , 220  , true) );
aClass.push_back(Aclass("John Doe3 ", 6.2 , 220  , true) );
aClass.push_back(Aclass("John Doe4 ", 6.2 , 220  , true) );

但无法复制您的错误。

此代码推回四次。

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

class Aclass
{
public:
    string  name;
    double   length;
    double   weight;
    bool    isEnabled;

    Aclass(){}
    ~Aclass(){}

    Aclass(string  _name, double   _length, double   _weight, bool    _isEnabled )  //constructor
    {
        name = _name;
        length = _length;
        weight = _weight;
        isEnabled = _isEnabled;

        cout<<" \n";
        cout<<"Name   : "<<name<<" \n";
        cout<<"length : "<<length<<" \n";
        cout<<"weight : "<<weight<<" \n";
        cout<<" \n";
    }



};



int main()
{ 

    vector<Aclass>  aClass;
    aClass.push_back(Aclass("John Doe ", 6.2 , 220  , true) );
    aClass.push_back(Aclass("John Doe2 ", 6.2 , 220  , true) );
    aClass.push_back(Aclass("John Doe3 ", 6.2 , 220  , true) );
    aClass.push_back(Aclass("John Doe4 ", 6.2 , 220  , true) );

    cout<<" \n";


return 0;
}

输出 :

Name   : John Doe
length : 6.2
weight : 220


Name   : John Doe2
length : 6.2
weight : 220


Name   : John Doe3
length : 6.2
weight : 220


Name   : John Doe4
length : 6.2
weight : 220


Press any key to continue

I tried out your code.

vector<Aclass>  aClass;
aClass.push_back(Aclass("John Doe ", 6.2 , 220  , true) );
aClass.push_back(Aclass("John Doe2 ", 6.2 , 220  , true) );
aClass.push_back(Aclass("John Doe3 ", 6.2 , 220  , true) );
aClass.push_back(Aclass("John Doe4 ", 6.2 , 220  , true) );

But could not replicate your error.

This code pushes back four times.

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

class Aclass
{
public:
    string  name;
    double   length;
    double   weight;
    bool    isEnabled;

    Aclass(){}
    ~Aclass(){}

    Aclass(string  _name, double   _length, double   _weight, bool    _isEnabled )  //constructor
    {
        name = _name;
        length = _length;
        weight = _weight;
        isEnabled = _isEnabled;

        cout<<" \n";
        cout<<"Name   : "<<name<<" \n";
        cout<<"length : "<<length<<" \n";
        cout<<"weight : "<<weight<<" \n";
        cout<<" \n";
    }



};



int main()
{ 

    vector<Aclass>  aClass;
    aClass.push_back(Aclass("John Doe ", 6.2 , 220  , true) );
    aClass.push_back(Aclass("John Doe2 ", 6.2 , 220  , true) );
    aClass.push_back(Aclass("John Doe3 ", 6.2 , 220  , true) );
    aClass.push_back(Aclass("John Doe4 ", 6.2 , 220  , true) );

    cout<<" \n";


return 0;
}

Output :

Name   : John Doe
length : 6.2
weight : 220


Name   : John Doe2
length : 6.2
weight : 220


Name   : John Doe3
length : 6.2
weight : 220


Name   : John Doe4
length : 6.2
weight : 220


Press any key to continue
预谋 2024-12-31 03:14:46

为什么字符串与它有任何关系?

您完成了 #include 吗?
您没有提到您的是否有复制构造函数。
它是这样的:

class A {
public:
        A() //default constructor
        A(const A&) //copy constructor
};

默认情况下,c++ 将复制所有公共成员,但您需要有一个复制构造函数才能使用 STL 容器和 push_back()

Why would the string have anything to do with it ?

Have you done #include <string> ?
You haven't mentioned whether your class has a copy constructor.
It goes something like this:

class A {
public:
        A() //default constructor
        A(const A&) //copy constructor
};

By default, c++ will copy all public members, but you need to have a copy constructor in order to use STL containers and push_back()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文