Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.

like image 922
Naxical Avatar asked Feb 06 '26 01:02

Naxical


1 Answers

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. Instead you are using the syntax to declare a,b, and c. When just referring to them you don't need the types. IE you should do:

  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.

like image 197
Doug T. Avatar answered Feb 08 '26 15:02

Doug T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!