C++ 错误:';l';不是一种类型
我用Linux编程,我有一个问题。我必须初始化两个大小为“l”的向量l'应该在命令行中给出 这是代码:C++ 错误:';l';不是一种类型,c++,vector,types,size,C++,Vector,Types,Size,我用Linux编程,我有一个问题。我必须初始化两个大小为“l”的向量l'应该在命令行中给出 这是代码: #include <iostream> #include <sys/shm.h> #include <sys/ipc.h> #include <sys/wait.h> #include <cstdlib> #include <cstdio> #include <
#include <iostream>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
using namespace std;
int l, m, n, Id;
struct vektori{
std::vector<long double> a(l);
std::vector<long double> b(l);
};
typedef struct vektori* vektor;
int main(int argc, char* argv[]){
if(argc!=4){
cout<<"Greska kod ulaznih parametara"<<endl;
return 0;
}
l=atoi(argv[1]);
m=atoi(argv[2]);
n=atoi(argv[3]);
vektor v;
Id=shmget(IPC_PRIVATE, sizeof(vektori), 0);
v=(vektor)shmat(Id, NULL, 0);
return 0;
}
将构造函数添加到
vektor
,并将l
作为参数。使用参数初始化成员
struct vektor // Assuming you meant to use vektor, not vektori
{
vektor(int l) : a(l), b(l) {}
std::vector<long double> a;
std::vector<long double> b;
};
你的结构定义应该只声明向量,你看起来像是在尝试初始化它们。在第14行和第15行,将
a(l)
更改为a
,将b(l)
更改为b
struct vektori{
std::vector<long double> a(l);
std::vector<long double> b(l);
};
(由于转换范围缩小,因此添加了显式强制转换。)
但是,不清楚为什么要使用全局变量来指定向量的初始大小。这不是一个好主意。为什么要在共享内存中使用
vector
s?它们不能在进程之间共享。产生这些消息的原因是,您正在声明两个函数,它们接受类型为l
的参数并返回std::vector
。我现在注意到了,谢谢,但我现在如何将内存段附加到“v”?@VinkoCerovečki,您是否正在询问如何使用shmget
,我不知道该怎么办it@VinkoCerovečki,我不熟悉shmget
的正确用法。我建议你在另一个SO帖子中问这个问题,并提供足够的细节。
vektor v(l);
struct vektori{
std::vector<long double> a(l);
std::vector<long double> b(l);
};
struct vektori{
std::vector<long double> a{ (long double) l };
std::vector<long double> b{ (long double) l };
};