错误:"L"不是类型
error: 'l' is not a type
我正在用Linux编程,我有一个问题。我必须初始化两个大小为"l"的向量。"l"应该被赋予表单命令行。
这是代码:
#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;
}
这是错误:
procesi.cpp:14:29: error: 'l' is not a type
procesi.cpp:15:29: error: 'l' is not a type
添加一个构造函数,以l
作为参数进行vektor
。
使用该参数初始化成员。
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;
};
然后,在 main
中使用:
vektor v(l);
struct vektori{
std::vector<long double> a(l);
std::vector<long double> b(l);
};
被编译器视为尝试将a
和b
声明为成员函数。并且使用l
代替参数类型。由于l
不是一种类型,因此这些成员声明的格式不正确。
如果要将 a
和 b
声明为类数据成员并立即将l
指定为初始值设定项,则必须使用基于 {}
的初始化语法
struct vektori{
std::vector<long double> a{ (long double) l };
std::vector<long double> b{ (long double) l };
};
(由于缩小了转换范围,因此添加了显式强制转换。
但是,目前尚不清楚为什么要使用全局变量来指定向量的初始大小。这不是一个好主意。
你的结构定义应该只声明向量,你看起来像是在尝试启动它们。 在第 14 行和第 15 行将a(l)
更改为 a
,将b(l)
更改为b
相关文章:
- 类没有命名C++代码中的类型错误
- 不完整类型错误(E0409、E0070、E0515)
- 使用具有结构不完整类型错误的模板
- 不命名构造函数和析构函数上的类型错误
- 返回派生类型时出现协变类型错误
- 为什么此构造函数没有给出不完整的类型错误?
- 定义模板化结构的特征时出现不完整的类型错误
- 使用重载构造函数时出现不完整的类型错误
- 为什么我得到表达式必须有类类型错误?
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 枚举成员不是类型错误
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 非默认析构函数会导致不完整的类型错误
- 为什么按引用传入会导致绑定引用类型错误
- Objective-C C++ 包装器 类型错误的不完整定义
- 输入类型错误
- 当构造函数的参数类型错误时引发异常
- 命名空间中的'bad_cast' 'std' 未命名类型错误
- C UNWORKOWN类型错误