I have a code here that needs to return a string from a function, but the compiler continues to tell me an error saying that "overloaded function differs only by return type from 'int Bible::getName(void)'". How can I fix this?

Here is my code:

// Joseph Yong
// CSC2430
// Homework 6

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

//Constructors
Bible::Bible()
{
	name = "";
	chapter = 0;
	verse = 0;
	testament = "";
	text = "";
}

Bible::Bible(string nam, int chapt, int vers, string testa, string txt) //Initialize to any size
{
	name = nam;
	chapter = chapt;
	verse = vers;
	testament = testa;
	text = txt;
}

//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()
{
	cout << "Enter the book name, chapter, and verse: ";
	cin >> name >> chapter >> verse >> endl;
	cout << "Enter in the testament: ";
	cin >> testament >> endl;
	cout << "Enter in the contents of the verse: ";
	cin >> text >> endl;
}

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

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

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

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

And here is my header code:

// Joseph Yong
// CSC2430
// Homework_6

//Header File
/*--------------------------------------------------------------------------*/
#ifndef HEADER
#define HEADER
#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, input;
	int chapter, verse;
};
#endif

Recommended Answers

All 11 Replies

Are both the posted code and the posted error message copy/paste from the code compiled? Cause I don't seen any function protocols listed int Bible::getName();

Are both the posted code and the posted error message copy/paste from the code compiled? Cause I don't seen any function protocols listed int Bible::getName();

Yes, they are directly from the compiler.

well I'm not seeing a getName() function that returns an int so I'm clueless.

so can anyone help me?? :(

fix this by removing the endl

cout << "Enter the book name, chapter, and verse: ";
cin >> name >> chapter >> verse >> endl;    
cout << "Enter in the testament: ";   
cin >> testament >> endl;   
cout << "Enter in the contents of the verse: ";   
cin >> text >> endl;

with this

cout << "Enter the book name, chapter, and verse: ";  
cin >> name >> chapter >> verse;    
cout << "Enter in the testament: ";   
cin >> testament;   
cout << "Enter in the contents of the verse: ";   
cin >> text;

and then this

string Bible::getTestament(string testament)
{
    return testament;
}

string Bible::getText(string text)
{
    return text;
}

with this

string Bible::getTestament()
{
    return testament;
}

string Bible::getText()
{
    return text;
}

fix this by removing the endl

cout << "Enter the book name, chapter, and verse: ";
cin >> name >> chapter >> verse >> endl;    
cout << "Enter in the testament: ";   
cin >> testament >> endl;   
cout << "Enter in the contents of the verse: ";   
cin >> text >> endl;

with this

cout << "Enter the book name, chapter, and verse: ";  
cin >> name >> chapter >> verse;    
cout << "Enter in the testament: ";   
cin >> testament;   
cout << "Enter in the contents of the verse: ";   
cin >> text;

and then this

string Bible::getTestament(string testament)
{
    return testament;
}

string Bible::getText(string text)
{
    return text;
}

with this

string Bible::getTestament()
{
    return testament;
}

string Bible::getText()
{
    return text;
}

end quote.

That does not solve anything as the same errors show up:

1>c:\users\joseph\documents\visual studio 2008\projects\homework_6\homework_6\source1.cpp(78) : error C2556: 'std::string Bible::getName(void)' : overloaded function differs only by return type from 'int Bible::getName(void)'
1>        c:\users\joseph\documents\visual studio 2008\projects\homework_6\homework_6\header1.h(29) : see declaration of 'Bible::getName'
1>c:\users\joseph\documents\visual studio 2008\projects\homework_6\homework_6\source1.cpp(78) : error C2371: 'Bible::getName' : redefinition; different basic types
1>        c:\users\joseph\documents\visual studio 2008\projects\homework_6\homework_6\header1.h(29) : see declaration of 'Bible::getName'

Line 10 of your Implementation file. You have an extraneous semi-colon in there. I don't think it would affect your issue, but remove it and see if it helps.

I don't see anything wrong with the declarations of name or Bible::getName().

I suspect the issue may be with your call. I suggest you post your calling statement(s) and the declaration of the variable(s) that are receiving the return values.

Well i compiled it.You have two memeber functions which are declared to take no arguments but instead they are .. taking an argument and returning it back .. how is that not visible?

<snipped to body>

>>[edit]Well i compiled it.You have two member functions which are declared to take no arguments but instead they are .. taking an argument and returning it back .. how is that not visible?
Since you didn't elaborate, I will elaborate for you:

string getTestament();
	string getText();

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
}

These don't match, which will cause a function overloading error (most likely manifested as a linker error), but not the one the OP is asking about. The implementations are the versions you'll want.

on line 10 you have a semicolon before your namespace decleration....

as far as your errors, when ever you see something that says "overloaded" I would make sure you really read over those functions and try to make them distinct. This has worked many times for me in the past

That's the problem though. The error is flagging an "overloaded" member function that isn't overloaded. If you look at the prototype and implementation, they match properly. That's why I suspect the issue is with the calling statement(s).

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.