I'm doing a homework assignment about constructors and this is really bothering me. I keep getting the error "constructors are not allowed a return type".

I HAVE put a semicolon after the class declaration.
I'll post my code here.

// Joseph Yong
// CSC2430
// Homework_6

//Header File
/*--------------------------------------------------------------------------*/

#include <iostream>
#include <string>
using namespace std;

class Bible
{
public:
	//Constructors
	Bible();
	Bible(string nam, int chapt, int vers, string testa, string txt);

	//Mutators
	void setName(string nam);
	void setChapter(int chapt);
	void setVerse(int vers);
	void setTestament(string testa);
	void setText(string txt);
	void setSetup1(string nam, int chapt, int vers, string testa);
	void setInput();

	//Accessors
	string getName();
	int getChapter();
	int getVerse();
	string getTestament();
	string getText();

	//Output
	void write1();
	void write2();

private:
	string name, testament, text;
	int chapter, verse;
};
// Joseph Yong
// CSC2430
// Homework 6

//Implementation File
/*----------------------------------------------------------------*/
#include <iostream>
#include <string>
using namespace std;
#include "Header1.h"

//Constructors
Bible::Bible()
:	chapter(0), verse(0)
{
}

Bible::Bible(string nam, int chapt, int vers, string testa, string txt) //Initialize to any size

//Mutators
void Bible::setName(string nam)
{
	name = nam;						//Change the string value of name
}

void Bible::setChapter(int chapt)
{
	chapter = chapt;				//Change the int value of chapter
}
	
void Bible::setVerse(int vers)
{
	verse = vers;					//Change the int value of verse
}

void Bible::setTestament(string testa)
{
	testament = testa;				//Change the string value of testament
}

void Bible::setText(string txt)
{
	text = txt;						//Change the string value of text
}

void Bible::setSetup1(string nam, int chapt, int vers, string testa)
{
	name = nam;
	chapter = chapt;				//Changes all data members
	verse = vers;
	testament = testa;				
}

string Bible::setInput(string ver)
{
	cout << "Enter in the verse text: ";
	cin >> ver;					//Changes the string value of input
	verse = ver;
}

//Accessors
int Bible::getName()
{
	return name;
}

int Bible::getChapter()
{
	return chapter;
}

int Bible::getVerse()
{
	return verse;
}

string Bible::getTestament()
{
	return testament;
}
	
int Bible::getText()
{
	return text;
}

//Output
void Bible::write1()
{
	cout << bib.getName << " " << bib.getChapter << ":" << bib.getVerse << " " << bib.getTestament << endl;
	cout << bib.getText;
}

Any help would be greatly appreciated!
Also, this code is yet to be finished. So in regards with "bib.getname" I understand that will bring me an error as I have yet to code main.cpp ;).

Thanks dani!

**This is my first post as I JUST joined daniweb ^^

Recommended Answers

All 10 Replies

the problem is line 18.

Bible::Bible(string nam, int chapt, int vers, string testa, string txt)

you dont have anything defined for this function. it should be

Bible::Bible(string nam, int chapt, int vers, string testa, string txt) 
{
       // stuff here
}

>>**This is my first post as I JUST joined daniweb ^^ <<

Welcome :) We're glad to have you apart of us.

why isn't this defined ?

Bible::Bible(string nam, int chapt, int vers, string testa, string txt) //Initialize to any size

I changed that but that did not solve anything.

could you post your new code?

could you post your new code?

I have not defined that one particular line of code you mentioned...I just put in brackets since I have not gotten to the part in the homework where I have to define that constructor.

Everything else remains the same.

i would just comment it out then by using the // in front of the definition and the decleration.

class Bible
{
public:
//Constructors
Bible();
//Bible(string nam, int chapt, int vers, string testa, string txt); here

//Mutators
//Bible::Bible(string nam, int chapt, int vers, string testa, string txt) //Initialize to any size   and here
// dont put any braces yet since you are not using it yet.

This is somewhat a guessing game without the exact code. But I'd suggest that you open the header file and scroll down to the very bottom of it, just to see that there absolutely isn't anything after the semicolon that ends the class declaration.

i would just comment it out then by using the // in front of the definition and the decleration.

class Bible
{
public:
//Constructors
Bible();
//Bible(string nam, int chapt, int vers, string testa, string txt); here

//Mutators
//Bible::Bible(string nam, int chapt, int vers, string testa, string txt) //Initialize to any size   and here
// dont put any braces yet since you are not using it yet.

Yeah, that's what I decided to do.
However, I keep getting a different error now. I'm trying to do this program one step at a time to see each of my functions work...however something is seriously going wrong.

Here is the code:

//Joseph Yong
//CSC2430
//Homework 6

//Client file
/*----------------------------------------------------------------*/

#include <iostream>
#include <string>
using namespace std;

#include "Header1.h"

int main()
{
	Bible bib;

	bib.setInput;

	return 0;
}
// Joseph Yong
// CSC2430
// Homework_6

//Header File
/*--------------------------------------------------------------------------*/

#include <iostream>
#include <string>
using namespace std;

class Bible
{
public:
	//Constructors
	Bible();
	Bible(string nam, int chapt, int vers, string testa, string txt);

	//Mutators
	void setName(string nam);
	void setChapter(int chapt);
	void setVerse(int vers);
	void setTestament(string testa);
	void setText(string txt);
	void setSetup1(string nam, int chapt, int vers, string testa);
	void setInput(string txt);

	//Accessors
	string getName();
	int getChapter();
	int getVerse();
	string getTestament();
	string getText();

	//Output
	void write1();
	void write2();

private:
	string name, testament, text, input;
	int chapter, verse;
};
// Joseph Yong
// CSC2430
// Homework 6

//Implementation File
/*----------------------------------------------------------------*/
#include <iostream>
#include <string>
using namespace std;
#include "Header1.h"

//Constructors
/*Bible::Bible()
:	name(""), chapter(0), verse(0), testament(""), text(""), input("")
{
}

Bible::Bible(string nam, int chapt, int vers, string testa, string txt) //Initialize to any size
{
}

//Mutators
void Bible::setName(string nam)
{
	name = nam;						//Change the string value of name
}

void Bible::setChapter(int chapt)
{
	chapter = chapt;				//Change the int value of chapter
}
	
void Bible::setVerse(int vers)
{
	verse = vers;					//Change the int value of verse
}

void Bible::setTestament(string testa)
{
	testament = testa;				//Change the string value of testament
}

void Bible::setText(string txt)
{
	text = txt;						//Change the string value of text
}

void Bible::setSetup1(string nam, int chapt, int vers, string testa)
{
	name = nam;
	chapter = chapt;				//Changes all data members
	verse = vers;
	testament = testa;				
}*/

void Bible::setInput(string txt)
{
	cout << "Enter in the verse text: ";
	cin >> txt;				
}

//Accessors
/*string Bible::getName()
{
	return name;
}

int Bible::getChapter()
{
	return chapter;
}

int Bible::getVerse()
{
	return verse;
}

string Bible::getTestament()
{
	return testament;
}
	
int Bible::getText()
{
	return text;
}

//Output
void Bible::write1()
{
	cout << bib.getName << " " << bib.getChapter << ":" << bib.getVerse << " " << bib.getTestament << endl;
	cout << bib.getText;
}*/

This is really frustrating me...

Oh and also, it's frustrating cause i keep getting errors.
It says that 'Bible' followed by 'void' is illegal in the source.cpp.
I do not have void following Bible ANYWHERE. what is WRONG with this compiler?

i think that is because you commented out you bible constructor. also in main you are doing bib.setInput; when it should be something like

string foo;
Bible bib;
bib.setInput(foo);
return 0;
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.