为什么这种向量分配不被接受?
所以我在类头中声明了一个向量,如下所示:
...
private:
vector<Instruction> instructions;
...
然后在构造函数中的 .cpp 实现中,我尝试像这样初始化它:
instructions = new vector<Instruction>();
Xcode 告诉我: 没有可行的重载 '='
我是基本上试图让这个类的行为像我在java中所期望的那样,其中类的实例保留这个向量。这就是为什么我想使用 new 来动态分配它,以确保它不会在堆栈或其他东西上丢失。任何帮助将不胜感激,非常感谢。
So I have declared a vector in my class header like this:
...
private:
vector<Instruction> instructions;
...
Then in the .cpp implementation in the constructor, I try to initialize it like this:
instructions = new vector<Instruction>();
Xcode tells me: No viable overloaded '='
I am basically trying to get this class to behave like I would expect in java, where instances of the class retain this vector. Thats why I wanted to dynamically allocate it using new
, so as to make sure that it doesn't get lost on the stack or something. Any help would be appreciated with this, thanks so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为了完成您想要做的事情,
instructions = new vector()
行是完全没有必要的。只需将其删除即可。当类的实例被构造时,向量将自动被默认构造。另一种方法是将指令变成指针,但似乎没有任何理由在这里这样做。
In order to do what you're trying to do the
instructions = new vector<Instruction>()
line is entirely unnecessary. Simply remove it. The vector will automatically get default-constructed when an instance of your class gets constructed.An alternative is to make
instructions
into a pointer, but there doesn't appear to be any reason to do this here.当您编写时,
您已经实例化了类用户正在使用的任何内存模型的指令,例如
when you write
you already have instantiated instructions to whatever memory model the user of your class is using e.g.
在您的类中,您声明一个
std::vector
。new vector();
返回一个std::vector*
。In your class, you declare a
std::vector<Instruction>
.new vector<Instruction>();
returns you astd::vector<Instruction>*
.operator new
返回一个指针,因此类型不匹配。真正的问题是你正在做这件事。您有充分的理由动态分配该向量吗?我对此表示怀疑,只需完全忽略它,因为它将与您类型的实例一起分配。
operator new
returns a pointer, so you have a type mismatch.The real issue is the fact that you are doing it at all. Do you have a good reason for dynamically allocating that vector? I doubt it, just omit that entirely as it will be allocated along with instances of your type.
您有一个成员值,但您尝试从
vector*
初始化它。从vector
初始化它或将声明更改为指针。如果您选择第二条路线,则需要遵守三法则 。您可能还想从此列表中获得一本不错的 C++ 书籍。
另外,我认为您的标头中有一个
using namespace std;
,这很糟糕。You have a member value but you try to initialize it from a
vector<Instruction>*
. Initialize it fromvector<Instruction>
or change the declaration to a pointer. If you go down the second route, you need to observe the rule of three.You might also want to get a decent C++ book from this list.
Also, I think you have a
using namespace std;
in your header which is bad.除非您知道自己在做什么,否则不要在 C++ 中使用
new
。 (目前您还没有。)而是使用自动对象。您已经将
指令
定义为自动对象。您只需要像初始化它一样初始化它:不过,如果您只想调用默认构造函数,则构造函数初始化列表中的指令的初始化是可选的。因此,在这种情况下,这就足够了:
如果您想动态分配向量,则需要使指令成为指向向量的指针。但这很少有意义,因为向量已经动态分配其数据,但包装了它,因此您不必处理由此产生的丑陋细节。
这些细节之一是,如果类中有一个动态分配的对象,那么您将不得不担心该类的销毁、复制构造和复制分配。
正如 Kerrek 已经指出的那样,您需要一本好的 C++ 书籍才能正确学习 C++。 做出选择。
Do not use
new
in C++ unless you know what you are doing. (Which you do not, currently.)Instead use automatic objects. You already defined
instructions
to be an automatic object. You just need to init it as if it were one:The initialization of
instructions
in the constructor's initialization list is optional, though, if you only want to call the default constructor anyway. So in this case, this would be enough:If you wanted to dynamically allocate a vector, you would need to make
instructions
a pointer to a vector. But this rarely ever make sense, since the vector already allocates its data dynamically, but wraps this, so you do not have to deal with the ugly details resulting from this.One of those details is that, if you have a dynamically allocated object in a class, you will then have to worry about destruction, copy construction, and copy assignment for that class.
As Kerrek already pointed out, you will need to have a good C++ book in order to properly learn C++. Make your pick.
我认为您将 C++ 与 C# 的语法混淆了。
首先,与许多语言不同,在堆栈上分配的变量(例如您的变量)是通过调用默认构造函数来初始化的,所以我怀疑您所做的事情是不必要的。
其次,为了完成您想要做的事情,您使用以下语法:
但是,正如我所说,这可能是多余的(并且在非优化编译器上是浪费的,因为它可能同时调用构造函数和赋值运算符) 。在履行机构的回答中可以找到更好的方法。
第三,与 C# 不同,new 运算符在堆上分配内存并返回指向新分配数据的指针。您的变量
instructions
不是指针,因此会出现错误。I think you are confusing C++'s with C#'s syntax.
First, unlike in many languages, variables allocated on the stack (such as yours), are initialized by calling the default constructor, so I suspect that what you are doing is unnecessary.
Second, in order to do what you are trying to do, you use the following syntax:
however, as I said, this is likely redundant (and wasteful on a non-optimizing compiler as it might call both the constructor and the assignment operator). A much better way to do this is found in sbi's answer.
Third, unlike in C#, the
new
operator allocates memory on the heap and returns a pointer to the newly allocated data. Your variableinstructions
is not a pointer, thus the error.