Syntax error on calling Base Constructor

Hi!

I've been trying to create a derived class which calls the constructor of its parent, but I've been getting a syntax error. I've researched everywhere, but I'm not sure why it won't compile.

I've used the examples I found as closely as possible, but no luck. What's wrong with this code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#ifndef CIPHER_H_
#define CIPHER_H_

#include <stdlib.h>
#include <string>
#include "Conversion.h"

class Cipher
{
public:
	std::string text;

	Cipher (std::string text) {this->text = text;}

	virtual std::string encode(std::string key){return "";}
	virtual std::string decode(std::string key){return "";}
};

class Vigenere : public Cipher
{
public:
	Vigenere() : Cipher(std::string text);

	std::string encode(std::string key);
	std::string decode(std::string key);

};

#endif /* CIPHER_H_ */
1) You specify the initialisation list (where you pass arguments to the base class constructor) in the definition of the subclass constructor, not the declaration.

2) In any case, you don't seem to have properly copied the syntax of whatever you were looking at. The initialiser list is supposed to specify the argument that's being passed to the base class constructor. From what you've written, it looks as though you're trying to declare the parameter for the base class constructor - but that's unnecessarily, since you've already declared that at line 14.
You are using member initialization syntax for the Vignere ctor, try this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//C++11
#include <iostream>
#include <string>
class Cipher
{
public:
	std::string text;

	Cipher (std::string text) {this->text = text;}

	virtual std::string encode(std::string key){return "";}
	virtual std::string decode(std::string key){return "";}
};

class Vigenere : public Cipher
{
public:
	using Cipher::Cipher; // inherits all ctor's

	std::string encode(std::string key);
	std::string decode(std::string key);
};

int main()
{

}

edit: here's a link: http://stackoverflow.com/questions/9979194/what-is-constructor-inheritance
edit_2: if all your class' members and methods are public you could also consider using struct instead of class
Last edited on
Hi, thanks for the response!

(ctor is short for constructor, right?)

I've tried adding
using Cipher::Cipher;
but I'm getting another error: ../Cipher.h:29:16: error: using declaration cannot refer to a constructor

I think my Java is showing, I miss super() XD
For how to use initialiser lists to specify arguments to base class constructors, have a look at that StackOverflow link gunnerfunner posted. I don't think you need to worry about the part of that post that talks about the "using" statement, unless that's specifically necessary for what you want to do.

Also, the tutorial on inheritance on this site talks about the same thing and might be helpful:

http://www.cplusplus.com/doc/tutorial/inheritance/
../Cipher.h:29:16: error: using declaration cannot refer to a constructor
Also make sure your compiler is C++11 compliant as mentioned in my previous post
Topic archived. No new replies allowed.