I've been scratching my head for quite some time with the following compilation error and I was hoping somebody could shed some light...

I have the following in my header (.h) file:

//the mem_data class======================================//
class mem_data : public reader, public date { //multiple inheritance

 public:
  mem_data(int,int,std::string);
  ~mem_data();

 private:
  std::vector<reader> mem_store;

};

Then, I have the following in the .cc file where I the code for the constructor/destructor is located:

//the mem_data class======================================//
mem_data::mem_data(int start_date,int ndays,string symbol) 
  : reader(std::string), date::date(std::string) {
  cout<<start_date<<endl;
}

mem_data::~mem_data() {}

Basically, the mem_data class is trying to inherit from the reader and date class.

When I compile, I am getting the following error

../../library/mem_data/mem_data.cc: In constructor ‘mem_data::mem_data(int, int, std::string)’:
../../library/mem_data/mem_data.cc:17: error: expected primary-expression before ‘)’ token
../../library/mem_data/mem_data.cc:17: error: expected primary-expression before ‘)’ token

Does anybody see what I am missing? I don't understand where I have screwed up the syntax.

Dani AI

Generated

The compiler error comes from the mem‑initializer list. The initializer must contain expressions (variables, literals, or function calls), and base classes are initialized by their class name — not by writing the constructor with a scope qualifier or a type name as an argument. In other words, a type name like std::string is not a valid expression, and date::date(...) is not the correct way to call a base-class constructor in the initializer list.

A minimal corrected form (showing the pattern to follow) looks like this:

mem_data::mem_data(int start_date, int ndays, const std::string& symbol)
  : reader(symbol), date(symbol), mem_store()
{
    std::cout << start_date << '\n';
}

Notes and troubleshooting points:

  • The mem-initializer must pass actual values (here symbol) to the base constructors. Replacing a type name with the variable that holds the string fixes the "expected primary-expression" error.
  • Do not use date::date(...) in the initializer list; use the base-class name date(...).
  • As mentioned, a default constructor for reader is only needed if reader objects are default-constructed (for example via vector<reader>(n) or resize). If reader is non-copyable or lacks a default ctor, prefer storing pointers (e.g., std::vector<std::unique_ptr<reader>>) or constructing elements explicitly with push_back.
  • Remember initialization order: base classes are initialized before members and members are initialized in the order of declaration in the class, not the order in the initializer list.
  • Confirm the actual constructor signatures of reader and date and pass matching argument types (use const std::string& to avoid extra copies).

First of all you should provide a default constructor for your class as for the error you're getting you should take a better look at your initialization list keeping in mind that you should provide valid arguments for the constructors of the classes you are inheriting from.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.