1

so I am stuck at something that I think is easy.

Note: everything below is for C++11 and above.


Let us start. I have a class named "Employee". Its consctructor is as follows:

Employee::Employee(const string& first, const string& last, const string& ssn)
      : firstName(first), lastName(last), socialSecurityNumber(ssn) {}

Furthermore, when trying to create the object, in my main, I do the following:

void main()
{
string firstName;
string lastName;
string socialSec;
Employee salariedEmployee{firstName, lastName, socialSec};
}

and I get the error:

error: cannot declare variable 'salariedEmployee' to be of abstract type 'Employee'


then I tried to create my object as a pointer, as followes:

Employee *salariedEmployee{&firstName, &lastName, &socialSec};

and get the error:

error: scalar object 'salariedEmployee' requires one element in initializer


I do not understand what I am doing wrong. I was used to coding is previous versions of C++11 but I am trying to learn these new tricks of using curly braces (uniform initialization). What am I doing wrong (in both cases)?

P.S. I have googled a lot but I am very confused about what to do. Two of the resourses I saved are these (but have read much more stuff):

Metalzero2
  • 531
  • 2
  • 6
  • 17

2 Answers2

3

error: cannot declare variable 'salariedEmployee' to be of abstract type 'Employee'

This error is not related to the way you call your constructor. It just says you are trying to instantiate a type which has not been completely defined, some of its methods are pure virtuals.

For instance, this works:

#include <string>

struct Employee
{
    Employee(const std::string& first, const std::string& last, const std::string& ssn);

    std::string firstName;
    std::string lastName;
    std::string socialSecurityNumber;
};

Employee::Employee(const std::string& first, const std::string& last, const std::string& ssn)
    : firstName(first), lastName(last), socialSecurityNumber(ssn)
{}

int main()
{
    std::string firstName;
    std::string lastName;
    std::string socialSec;
    Employee bob{firstName, lastName, socialSec};
}

demo

But if you add a pure virtual fire method to Employee, it will fail to compile: demo.

YSC
  • 38,212
  • 9
  • 96
  • 149
  • You are right. In the Employee there is the like " virtual double earnings() const = 0; // pure virtual". It even says that it is a pure virtual but did not know exactly what that did and how it was used. Employee is indeed intended to be a base class for "SalariedEmployee" class. Thanks. – Metalzero2 Nov 23 '17 at 14:29
-1

Although it works fine on my computer. In your case the problem can be the uniform initialization. Basically, the constructor is a a special kind of class Method(function, sort-of).

So, the way to pass arguments to a function is by parentheses () ,not by uniform initialization.

aditya rawat
  • 154
  • 10
  • I down-voted your answer because this is simply not the case. The error message gives it away ;) – YSC Nov 23 '17 at 14:35