使用类的向量

发布于 2024-11-25 11:36:13 字数 2549 浏览 1 评论 0原文

我是 C++ 的新手,我刚刚开始使用称为类的容器。

我正在开发一个程序,其中定义了一个对象并将其存储在向量内。

程序如下,然后是我得到的错误的描述。

这是“car.h”

#ifndef CAR_H_GUARD
#define CAR_H_GUARD

class car{

    int v;//the velocity 
    int loc;//location

public:

    //default constructor, copy constr., cp. assgnmt. optr., destructor
    car();
    car(const car& j);
    car& operator=(const car& j);
    ~car();

    void set_v(int);    //sets velocity 
    void set_pos(int);  //sets position

    int get_v (){return((v));}//returns velocity
    int get_pos(){return(loc);}//returns position

};
#endif

这是“car.cpp”

#include "car.h"
#include <iostream>

using namespace std;

//constructor
car::car() {

    v=1;
    loc=0;

}

//copy constructor
car::car(const car& j){

    v=j.v;
    loc=j.loc;

}

//copy assignment operator

car& car::operator=(const car& j){

    v=j.v;
    loc=j.loc;

    return *this;

}

//destructor
car::~car () {

    delete &v;
    delete &loc;

}

//sets velocity
void car::set_v(int p){

    v = p;

}

//sets position
void car::set_pos(int l){

    loc = l;

}

我试图通过复制赋值运算符来创建“移动构造函数”。这是为了让以下涉及我的对象向量的代码能够工作。

#include "main.h"
#include "car.h"
#include <iostream>
#include <vector>

using namespace std;

//update car position
void update (vector <car> &cr){

    //changes state of cars, eg.
    for (int c = 0; c<cr.size();c++){
        cr[c].set_v(1);
        cr[c].set_pos(100);
    }

}

int main(){

    //a vector of cars
    vector<car> cat;

    car j;
    j.set_v(100);
    j.set_pos(99);
    cat.push_back(j);

    //new cars enters the road
    j.set_v(120);
    j.set_pos(77);
    cat.push_back(j);

    j.set_v(80);
    j.set_pos(55);
    cat.push_back(j);

    //updating cars
    update(cat);

    //output
    for (int i = 0; i<cat.size(); i++){

        cout << cat[i].get_v()<<" is the velocity of car "<<i<<".\n";
        cout << cat[i].get_pos()<<" is the position of car "<<i<<".\n";

    }
}

我在 Internetz 上查找了以下错误。

main(8852) malloc: * * * 对象 0x7fff5fbff630 的错误:未分配正在释放的指针 * * * 在malloc_error_break中设置断点进行调试 中止陷阱

虽然这可能是编译器不兼容(我在 Xcode 中编写...并在 mac 10.6.8 上使用 g++...),但是当向量中只有一辆车时,代码确实会给出输出。当我尝试引入其他汽车时,我只收到 malloc 错误。我想知道我是否遗漏了类定义的某些内容,或者我以某种方式搞砸了数据类型或指针引用。如果我尝试使用更新函数对其进行非平凡的操作,那么输出的值也会出现问题。

我什至尝试将指针向量(指向类)传递给更新函数;我仍然无法将新元素插入向量中。这个网站对帮助我学习类结构及其指针非常有帮助。

感谢您阅读这个很长的问题。

I am a newbie at C++, and I am just starting to work with the container called classes.

I am working on a program where an object is defined, and is stored inside a vector.

The program is as follows, and then the description of the error I got.

This is "car.h"

#ifndef CAR_H_GUARD
#define CAR_H_GUARD

class car{

    int v;//the velocity 
    int loc;//location

public:

    //default constructor, copy constr., cp. assgnmt. optr., destructor
    car();
    car(const car& j);
    car& operator=(const car& j);
    ~car();

    void set_v(int);    //sets velocity 
    void set_pos(int);  //sets position

    int get_v (){return((v));}//returns velocity
    int get_pos(){return(loc);}//returns position

};
#endif

This is "car.cpp"

#include "car.h"
#include <iostream>

using namespace std;

//constructor
car::car() {

    v=1;
    loc=0;

}

//copy constructor
car::car(const car& j){

    v=j.v;
    loc=j.loc;

}

//copy assignment operator

car& car::operator=(const car& j){

    v=j.v;
    loc=j.loc;

    return *this;

}

//destructor
car::~car () {

    delete &v;
    delete &loc;

}

//sets velocity
void car::set_v(int p){

    v = p;

}

//sets position
void car::set_pos(int l){

    loc = l;

}

I have tried to create the "move constructor", by having a copy assignment operator. This is for the following code involving a vector of my objects to work.

#include "main.h"
#include "car.h"
#include <iostream>
#include <vector>

using namespace std;

//update car position
void update (vector <car> &cr){

    //changes state of cars, eg.
    for (int c = 0; c<cr.size();c++){
        cr[c].set_v(1);
        cr[c].set_pos(100);
    }

}

int main(){

    //a vector of cars
    vector<car> cat;

    car j;
    j.set_v(100);
    j.set_pos(99);
    cat.push_back(j);

    //new cars enters the road
    j.set_v(120);
    j.set_pos(77);
    cat.push_back(j);

    j.set_v(80);
    j.set_pos(55);
    cat.push_back(j);

    //updating cars
    update(cat);

    //output
    for (int i = 0; i<cat.size(); i++){

        cout << cat[i].get_v()<<" is the velocity of car "<<i<<".\n";
        cout << cat[i].get_pos()<<" is the position of car "<<i<<".\n";

    }
}

And I get the following error, which I looked up on the Internetz.

main(8852) malloc: * * * error for object 0x7fff5fbff630: pointer being freed was not allocated
* * * set a breakpoint in malloc_error_break to debug
Abort trap

Although it might be a compiler incompability (I wrote in Xcode... and uses g++, on mac 10.6.8 ...), the code however does give an output when there is only one car in the vector. When I tried to introduce the other cars, I receive only the malloc error. I was wondering whether I am missing something for the class definition, or that somehow I botched up the data-types, or pointer referencing. There is also an issue with the values outputted if I tried to non-trivially manipulate it with an update function.

I have even tried to pass instead a vector of pointers (to the class) to the update function; I still have trouble inserting new elements into the vector. This site was very helpful in helping me learn the class structure, and its pointers.

Thanks for reading this long problem.

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

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

发布评论

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

评论(1

无声情话 2024-12-02 11:36:13

仅对使用过new 的事物使用delete。您永远删除未由new单独分配的内容。 Car 析构函数中的这些行...非常具有破坏性(请原谅我的双关语):

delete &v;
delete &loc;

删除这些行 - 它们完全没有必要,它们是问题的根源。在析构函数中使用 delete 的唯一一次是当您在构造函数中使用 new 分配对象时,或者当指针指向对象被传递给您的构造函数,并理解您的对象负责在对象被破坏时删除指针。

One uses delete only on things where one has used new. You never delete something that wasn't separately allocated by new. These lines in your Car destructor are... very destructive (pardon my pun):

delete &v;
delete &loc;

Delete these lines -- they're totally unnecessary and they're the source of your problem. Just about the only time you'd use delete in a destructor is when you've used new to allocate an object in your constructor -- or when a pointer-to-object is passed to your constructor with the understanding that your object is responsible for deleting the pointer when your object is destructed.

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