在类的私有部分声明数组
我有一个类,该类的部分输入是一个可变长度的向量(称为数据)(假设它的长度为 N)。我已将其包含在函数之后:
N = data_->size();
在类的私有部分中,我想声明一个数组 double A[N][N];
。然而,当我尝试这样做时,我听到一些话
错误:“N 不是类型名称、静态或枚举数”。
如何创建数组 A[N][N]
?
抱歉,如果这已经在其他地方解释过,因为我对 C++ 很陌生,所以甚至不知道要寻找什么!
编辑 - 附加代码:
class foo {
public:
foo (std::vector &data)
: data(data_)
{
N = data_->size();
M = /* four times the last member of data (which is a vector of positive integers)*/
}
private:
double A[M][M];
void foo(void)
{
for (std::size_t i=1; i<=M; ++i)
{
A[i][i] = 1;
}
}
};
希望这有某种意义......我如何能够定义 A[M][M]?也许对 M 来说这是不可能的,因为 M 是数据的函数。如果M不可能,N也可能吗?
我能想到的一种可能性是我可以将 A 设为 std::vector< std::vector
然后将很多 0 或其他内容推入其中,然后修改值...
I've got a class, and part of the input into the class is a vector (called Data) of variable length (lets say it has length N). I've included this after the function:
N = data_->size();
In the private section of the class, I want to declare an array double A[N][N];
. However, when I try to do this, I get something saying
error: "N is not a type name, static, or enumerator".
How do I create the array A[N][N]
?
Sorry if this is already explained somewhere else, as I'm very new to c++, so wouldn't even know what to look for!
Edit -- attached code:
class foo {
public:
foo (std::vector &data)
: data(data_)
{
N = data_->size();
M = /* four times the last member of data (which is a vector of positive integers)*/
}
private:
double A[M][M];
void foo(void)
{
for (std::size_t i=1; i<=M; ++i)
{
A[i][i] = 1;
}
}
};
Hope that makes some sort of sense... How would I be able to define A[M][M]? Maybe it's not possible to do it for M as M is a function of the data. If not possible for M, is it possible for N?
One possibility I can think of is that I can make A a std::vector< std::vector<double> > A
and then push a lot of 0's or something into it, and THEN modify the values...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 std::vector 类,则必须在 data_ 类的函数(例如构造函数)中创建向量,使用以下句子:
括号的第一个参数是向量的大小,第二是它的数据类型。
抱歉我的英语,我是西班牙人,而且我的英语不是很好。
if you´re using the std::vector class you must creates the vector in a function of the data_ class (like the constructor, for example), using this sentence:
The first parameter of the parentheses is the size of the vector and the second is the type of data on it.
Sorry for my english, i´m spanish and my english isn´t very good.
你不能那样做。数组是类型,必须在编译时知道它们。这包括它们的尺寸。所以你不能将动态数组作为类的一部分。您得到的最接近的东西是指向手动分配的数组的指针,但实际上它本质上是一个 std::vector 。因此,也许最简单的解决方案是只拥有一个向量向量,或者可能是一个大小为
N * N
的向量,您可以按j + N * i
的步幅访问它。示例:
或者:
访问:
v[2][4] = 8;
更新: 由于您编辑了答案,您可以编写类似这样的内容来获取
N * 4n
向量,其中data.back() == n
:You cannot do that. Arrays are types, and they have to be known at compile time. This includes their sizes. So you cannot have a dynamic array as part of your class. The closest thing you get is a pointer to manually allocated array, but that is in fact essentially a
std::vector
. So perhaps the easiest solution is to just have a vector of vectors, or perhaps a single vector of sizeN * N
that you access in stridesj + N * i
.Example:
Or:
Access:
v[2][4] = 8;
Update: Since you edited your answer, you can write something like this to get you an
N * 4n
vector, wheredata.back() == n
:嗯...这里有几个问题...
N
和M
均未声明(在构造函数中或作为foo
的成员data
(您可能指的是data_(data)
,因为语法是member(expression)
,而不是表达式(member))
std::vector<某种类型>
;如果你希望它是通用的,你需要使用模板或从 boost 获得一些帮助A
)void foo(void)
是一个问题,因为它不是构造函数(没有类型)的正确语法,而是使用类名让我们构建更接近您想要的内容
从类
foo
开始:构造函数采用单个类型参数
std::vector
你想用数据初始化一个成员变量
此时我会注意到,我通常不会把非-的定义类声明中的简单构造函数,而是在实现文件中,因此我可以将成员变量的声明与构造函数的声明一起放在公共部分下方。但为了简洁起见,我将在这里保留这种方式。
您想要捕获并存储数据的大小
};
此时,我们还没有制作出您想要的多维存储,但是现在您需要做出一些有关如何管理存储的决定。如果您使用 Kerrek SB 的方法,这看起来像
Hmmm...several issues here...
N
norM
are declared (either in the constructor or as members offoo
)data
that is not declared (and you may meandata_(data)
as the syntax ismember(expression)
, notexpression(member))
std::vector< sometype >
; if you want it to be generic you'll need to use templates or get some help from boostA
)void foo(void)
is a problem because it is not the correct syntax for a constructor (which has no type) but uses the class nameLet's build up to something closer to what you want
Start with a class
foo
:with a constructor taking a single argument of type
std::vector<double>
you want to initialize a member variable with the data
At this point I'll note that I would generally not put the definition of a non-trivial constructor in the class declaration, but in a implementation file instead, and consequently I would be able to put the declaration of member variable beneath the public section with the declaration of the constructor. But for compactness I'll leave this way here.
You want to capture and store the size of the data
};
At this point we still haven't made that multi-dimensional storage that you want, but now you have some decisions to make about how to manage the storage. If you use Kerrek SB's approach this looks something like
如果您不想自己管理内存,则需要使用智能指针动态创建它。
在构造函数中:
并不是说您必须在析构函数中调用delete,现在要遵守三规则,您必须有一个复制构造函数和一个赋值运算符...这里使用智能指针更好:请参阅 Boost 智能指针帮助页面。
You need to create it dynamically, using a smart pointer if you do not want to manage memory yourself.
in the constructor:
Not that you have to call delete in your destructor and now to obey the rule of three you have to have a copy constructor and a assignment operator... Using a smart pointer is better here: see the Boost smart pointer help page.