错误:"L"不是类型

error: 'l' is not a type

本文关键字:类型 错误      更新时间:2023-10-16

我正在用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);
};

被编译器视为尝试将ab声明为成员函数。并且使用l代替参数类型。由于l不是一种类型,因此这些成员声明的格式不正确。

如果要将 ab 声明为类数据成员并立即将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