简体   繁体   English

C ++错误 - 'char'和'int'之前的预期主表达式

[英]C++ error - expected primary expression before 'char' and 'int'

#include<iostream>
#include<cstring>
using namespace std;
class Employee
{
    char name[5];
    int id;
    int age;
    public:
    Employee(char* a, int b, int c)
    {
        strcpy(name, a);
        id=b;
        age=c;
    }
};
class Officer: public Employee
{
    char officer_cadre[3];
    public:
    Officer(char* a, int b, int c, char* d):Employee(char* a, int b, int c)
    {
        strcpy(officer_cadre, d);
    }
};
int main()
{
   Officer o1("Nakul", 1, 2, "ABC");
   return 0;
}

The above code is simple, but I'm not able to figure out why the compiler is throwing errors like 'expected primary expression before char' and 'expected primary expression before int'. 上面的代码很简单,但是我无法弄清楚为什么编译器会抛出'char之前的预期主表达式'和'int之前的期望主表达式'之类的错误。

Change this line: 改变这一行:

Officer(char* a, int b, int c, char* d):Employee(char* a, int b, int c)

To this: 对此:

Officer(char* a, int b, int c, char* d):Employee(a,b,c)

Also I am concerned about your declaration of officer_cadre. 我也很关注你对officer_cadre的声明。 It's an array of character pointers, but there's no memory allocation. 它是一个字符指针数组,但没有内存分配。 Was that the declaration you meant? 那是你的意思吗?

On this line 在这条线上

  Officer(char* a, int b, int c, char* d):Employee(char* a, int b, int c)

You should just pass a,b, and c. 你应该只传递a,b和c。 Instead you are using the syntax to declare a,b, and c. 相反,您使用语法来声明a,b和c。 When just referring to them you don't need the types. 当只是提到它们时,你不需要这些类型。 IE you should do: 你应该做的IE:

  Officer(char* a, int b, int c, char* d):Employee(a, b, c)

You may have just accidentally copy-pasted the declaration into the child class's constructor. 您可能只是意外地将声明复制粘贴到子类的构造函数中。

Change 更改

char* officer_cadre[3];

to

char officer_cadre[3];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM