Declaration is incompatible with type - c++

header file:
#ifndef H_bankAccount;
#define H_bankAccount;
class bankAccount
{
public:
string getAcctOwnersName() const;
int getAcctNum() const;
double getBalance() const;
virtual void print() const;
void setAcctOwnersName(string);
void setAcctNum(int);
void setBalance(double);
virtual void deposit(double)=0;
virtual void withdraw(double)=0;
virtual void getMonthlyStatement()=0;
virtual void writeCheck() = 0;
private:
string acctOwnersName;
int acctNum;
double acctBalance;
};
#endif
cpp file:
#include "bankAccount.h"
#include <string>
#include <iostream>
using std::string;
string bankAccount::getAcctOwnersName() const
{
return acctOwnersName;
}
int bankAccount::getAcctNum() const
{
return acctNum;
}
double bankAccount::getBalance() const
{
return acctBalance;
}
void bankAccount::setAcctOwnersName(string name)
{
acctOwnersName=name;
}
void bankAccount::setAcctNum(int num)
{
acctNum=num;
}
void bankAccount::setBalance(double b)
{
acctBalance=b;
}
void bankAccount::print() const
{
std::cout << "Name on Account: " << getAcctOwnersName() << std::endl;
std::cout << "Account Id: " << getAcctNum() << std::endl;
std::cout << "Balance: " << getBalance() << std::endl;
}
Please help i get an error under getAcctOwnersName, and setAcctOwnersName stating that the declaration is incompatible with "< error-type > bankAccount::getAcctOwnersName() const".

You need to
#include <string>
in your bankAccount header file, and refer to the strings as std::string.
#ifndef H_bankAccount;
#define H_bankAccount;
#include <string>
class bankAccount
{
public:
std::string getAcctOwnersName() const;
....
once it is included in the header, you no longer need to include it in the implementation file.

I've found that when a private member variable and a member function have the same name the IDE gives me the "incompatible" error, perhaps that is what you are experiencing...

Sometimes this error occur because it's vary from machine to machine. Your program will work fine if you declare your class and all of its implementations in one file instead doing declaration of class in other file and linked it with your driver file.
Again: This is totally machine dependent error.
In visual studio 2012 you will face this kind of error because it not work for these files while in other versions of vs you will not face any error type exception.
Hope it's worth.....

Related

getting "error: redefinition of class" in cpp file even though I am not trying to redefine a class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I was coding something as a little practice and I kept getting this error:
error: redefinition of a "LabMouse::LabMouse(std::__cxx::string,int)
It's saying that it was already defined in my .h file, which it is. I'm just confused on why it's saying that I'm trying to redefine it within my .cpp file?
The .cpp file
#include <iostream>
#include "LabMouse.h"
LabMouse::LabMouse(const std::string aname, const int aage){
}
void LabMouse::speak(){
std::cout << "Narf!" << std::endl;
}
LabMouse::~LabMouse(){
std::cout << "LabMouse Destructor" << std::endl;
}
void LabMouse::setName(std::string name){
name = "Pinky";
}
void LabMouse::sayHello(){
std::cout << "LabMouse Name: " << aname << " ,age: " << aage << std::endl;
}
The .h file
#ifndef LABMOUSE_H
#define LABMOUSE_H
#include <string>
class LabMouse{
private:
std::string aname = "";
int aage = 1;
public:
LabMouse(const std::string aname, const int aage){
}
void speak();
void setName(std::string aname);
void sayHello();
virtual ~LabMouse();
};
#endif
In the header file remove the braces (thereby turning your definition into a declaration), otherwise you are providing an implementation
LabMouse(const std::string aname, const int aage){
}
should just be
LabMouse(const std::string aname, const int aage);
because you have an implementation already in the cpp file.
You are defining a body for LabMouse() in the .h file, and you are defining another body for LabMouse() in the .cpp file. Hence the error. You need to get rid of one of those bodies.
Also, your constructor is not initializing the aname and aage class members with the values of the input parameters.
Use this instead:
#include <iostream>
#include "LabMouse.h"
LabMouse::LabMouse(const std::string aname, const int aage)
: name(aname), age(aage) {
}
void LabMouse::speak() const {
std::cout << "Narf!" << std::endl;
}
LabMouse::~LabMouse() {
std::cout << "LabMouse Destructor" << std::endl;
}
void LabMouse::setName(const std::string &aname) {
name = aname;
}
void LabMouse::sayHello() const {
std::cout << "LabMouse Name: " << name << ", age: " << age << std::endl;
}
#ifndef LABMOUSE_H
#define LABMOUSE_H
#include <string>
class LabMouse{
private:
std::string name;
int age = 1;
public:
LabMouse(const std::string aname, const int aage);
virtual ~LabMouse();
void speak() const;
void setName(const std::string &aname);
void sayHello() const;
};
#endif
Or this:
#include <iostream>
#include "LabMouse.h"
void LabMouse::speak() const {
std::cout << "Narf!" << std::endl;
}
LabMouse::~LabMouse() {
std::cout << "LabMouse Destructor" << std::endl;
}
void LabMouse::setName(const std::string &aname) {
name = aname;
}
void LabMouse::sayHello() const {
std::cout << "LabMouse Name: " << name << ", age: " << age << std::endl;
}
#ifndef LABMOUSE_H
#define LABMOUSE_H
#include <string>
class LabMouse{
private:
std::string name;
int age = 1;
public:
LabMouse(const std::string aname, const int aage)
: name(aname), age(aage)
{
}
virtual ~LabMouse();
void speak() const;
void setName(const std::string &aname);
void sayHello() const;
};
#endif

Class variable not declared in this scope error c++

I'm having problem with the function DisplayValues() in this program. First, I'm asked to write the prototype only for the function inside the class. Second requirement is to place the entire function DisplayValues() outside the class. DisplayValues() is a const inline member function.
All the above needs to be in a header file. I've written below and got errors "'accountType' not declared in this scope" for the DisplayValues() function. I did search for solutions but none worked without modifying the requirements above. Would anyone please advise?
My codes:
File SavingsAccount.h
ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
class SavingsAccount
{
private:
int accountType;
public:
inline void DisplayValues() const;
};
inline void DisplayValues()
{
cout << "Account type: " << accountType << '\n'
}
#endif //SAVINGSACCOUNT_H
File SavingsAccount.cpp
#include <iostream>
#include "SavingsAccount.h"
using namespace std;
void SavingsAccount::GetInitialValues()
{
cout << "Enter account type:\n";
cin >> accountType;
}
File main.cpp
#include <iostream>
#include "SavingsAccount.h"
using namespace std;
int main()
{
SavingsAccount ac;
ac.GetInitialValues();
ac.DisplayValues();
return 0;
}
Your code has 3 obvious problems that I can see:
This is a free function, not a class member function.
inline void DisplayValues()
{
cout << "Account type: " << accountType << '\n'
}
Fix:
inline void SavingsAccount::DisplayValues()
{
cout << "Account type: " << accountType << '\n'
}
Your declaration is inline void DisplayValues() const; but definition is missing the const keyword.
inline void SavingsAccount::DisplayValues()
^^^^^ const missing here
You are missing a semicolon is this line:
cout << "Account type: " << accountType << '\n'
^^ semicolon missing here

problem with constructors and g++ compile recipe

I'm creating a cpp program using functions that are applied to C++11. Even though the code seems correct and has no syntax errors i'm getting this message when i compile:
/tmp/cce9dpew.o: In function `Object::Object()':
classes.cpp:(.text+0xd): undefined reference to `vtable for Object'
/tmp/cce9dpew.o: In function `Object::~Object()':
classes.cpp:(.text+0x45): undefined reference to `vtable for Object'
/tmp/cce9dpew.o:(.rodata._ZTI6String[_ZTI6String]+0x10): undefined reference to `typeinfo for Object'
collect2: error: ld returned 1 exit status
I have to add here that if i put all those .cpp and .h files in one it runs Aok printing constructor and destructor cout's just fine.
Can someone help?The code is below.
compile recipe i used to run them all together: g++ -std=c++0x classes.h classes.cpp mainiz.cpp
classes.h:
#ifndef CLASSES_H
#define CLASSES_H
#include <iostream>
#include <cstring>
using namespace std;
class Object
{
private:
int id;
public:
Object();
~Object();
void set_id(int ids);
int get_id();
void Equal(Object* bj) const;
void Identical(Object* bj) const;
virtual Object* clone();
virtual void toString();
};
class String:public Object
{
string characters;
public:
String();
~String();
void set_char(string a);
string get_char();
String* clone();
void toString();
int Length();
void Clear(string a);
string& Concat(string &a);
char At(char b);
string& UpdateAt(string a,string charact);
void Print(const string a) const;
};
#endif //CLASSES_H
classes.cpp:
#include <iostream>
#include <cstring>
#include "classes.h"
using namespace std;
//FOR OBJECT CLASS
Object::Object(){ cout << "An object just got created." << endl;}
Object::~Object(){ cout << "An object just got destroyed." << endl; }
void Object::set_id(int ids) { this->id = ids; }
int Object::get_id() { return this->id;}
void Object::Equal(Object* bj) const
{
if((this->id == bj->id))
{
cout << "The objects are equal." << endl;
}
else
{
cout << "The objects are not equal." <<endl;
}
}
void Object::Identical(Object* bj) const
{
if(this==bj)
{
cout << "The objects are identical." <<endl;
}
else
{
cout << "The objects are not identical." <<endl;
}
}
//FOR STRING CLASS
String::String(){ cout << "String just created" << endl;}
String::~String(){ cout << "String to be destroyed" << endl;}
void String::set_char(string a) { this->characters = a;}
string String::get_char() { return this->characters;}
String* String::clone() { return this;}
void String::toString() {cout << "characters" << endl;}
int String::Length()
{
string a = this->characters;
return a.length();
}
void String::Clear(string a)
{
this->characters.clear();
}
string& String::Concat(string &a){ return (this->characters.append(a));}
char String::At(char b) { return (this->characters.find(b)); }
string& String::UpdateAt(string a,string charact)
{
int position=this->characters.find(charact);
return this->characters.replace(position,1,a);
}
void String::Print(const string a) const { cout << "print of string:" << a << endl; }
mainiz.cpp:
#include <iostream>
#include <cstring>
#include "classes.h"
using namespace std;
int main()
{
Object k;
Object *st = new String;
String d;
}
Making the destructor for Object class "virtual" you would get another error for undefined reference to Object::clone and Object::toString.
You can try what #Igor suggested, but your current mainiz.cpp code won't work because C++ doesn't allow an instance of a class with pure virtual methods.
You can try the following code:
class Object {
virtual ~Object();
virtual Object* clone();
virtual void toString();
};
Object* Object::clone() {
// Make your implementation here
return nullptr;
}
void Object::toString() {
// Make your implementation here
}
Object::clone and Object::toString are declared but never implemented.
If you want to leave them unimplemented, make them pure virtual, as in
class Object {
virtual Object* clone() = 0;
};
None of the solutions given above were correct.The problem was within my compilation recipe.These functions started existing after C++11 so if you're using something like that your compilation recipe should be:
g++ -g -std=c++11 -o executable file.cpp main.cpp

Struggling with C++ "was not declared in this scope"

Can anyone tell me why i get the error "name was not declared in the scope when running this?
Thanks.
class lrn11_class{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string lrn11_name;
};
int main()
{
lrn11_class lrn11_nameobject;
lrn11_nameobject.setname("Zee");
cout << lrn11_nameobject.getname() << endl;
return 0;
}
This should work - see comments (BTW use std:: - Why is "using namespace std" considered bad practice?)
#include <iostream>
#include <string>
class lrn11_class{
public:
void setName(const std::string& x){ // Potentially saves copying overhead
name = x;
}
std::string getName() const { // Look up const and its uses
return name;
}
private:
std::string name; // - Used: string lrn11_name; but functions use name!
};
int main()
{
lrn11_class lrn11_nameobject;
lrn11_nameobject.setName("Zee"); // Fixed typo
std::cout << lrn11_nameobject.getName() << std::endl; // Ditto
return 0;
}
You have declare lrn11_name as a member varible for this class. But in set and get functions you are using name.
Other than than you need to call functions as you have defined.
so instead of :-
lrn11_nameobject.setname("Zee");
cout << lrn11_nameobject.getname() << endl;
You have to use following code :-
lrn11_nameobject.setName("Zee");
cout << lrn11_nameobject.getName() << endl;
Make sure that
#include <iostream>
using namespace std;
should be included.

Trouble with abstract classes in c++

main:
#include <iostream>
#include <string>
#include "serviceChargeChecking.h"
int main()
{
serviceChargeChecking newAccount("Crim", 111222, 50.00, 100, 1.00);
system("PAUSE");
return 0;
}
serviceChargeChecking.h:
#ifndef H_serviceChargeChecking
#define H_serviceChargeChecking
#include "checkingaccount.h"
#include <string>
class serviceChargeChecking: public checkingAccount
{
public:
void setMonthlyFee(double);
void writeCheck(int);
void getMonthlyStatement() const;
serviceChargeChecking(std::string =" ",int = 0, double = 0.00, int= 0, double = 0.00);
private:
double serviceCharge;
};
#endif
serviceChargeChecking.cpp:
#include "serviceChargeChecking.h"
#include <iostream>
using std::string;
void serviceChargeChecking::setMonthlyFee(double fee)
{
serviceCharge=fee;
}
void serviceChargeChecking::getMonthlyStatement() const
{
checkingAccount::getMonthlyStatement();
std::cout<< "Service Charge: " << serviceCharge << std::endl;
}
void serviceChargeChecking::writeCheck(int ammount)
{
if(checkingAccount::getChecks()>0)
{
checkingAccount::setChecks(checkingAccount::getChecks()-ammount);
}
else
{
std::cout<<"No checks available." << std::endl;
}
}
serviceChargeChecking::serviceChargeChecking(string name, int acct, double bal, int numCheck, double sCharge)
{
bankAccount::setAcctOwnersName(name);
bankAccount::setAcctNum(acct);
bankAccount::setBalance(bal);
checkingAccount::setChecks(numCheck);
serviceCharge=sCharge;
}
checkingAccount.h:
#ifndef H_checkingAccount
#define H_checkingAccount
#include "bankAccount.h"
#include <iostream>
class checkingAccount: public bankAccount
{
public:
virtual void writeCheck()=0;
void deposit(double);
void withdraw(double);
void getMonthlyStatement() const;
int getChecks();
void setChecks(int);
private:
int numChecks;
};
#endif
checkingAccount.cpp:
#include "checkingAccount.h"
#include <iostream>
int checkingAccount::getChecks()
{
return numChecks;
}
void checkingAccount::setChecks(int c)
{
numChecks=c;
}
void checkingAccount::deposit(double d)
{
bankAccount::setBalance(bankAccount::getBalance()+d);
}
void checkingAccount::withdraw(double w)
{
bankAccount::setBalance(bankAccount::getBalance()-w);
}
void checkingAccount::getMonthlyStatement() const
{
bankAccount::getMonthlyStatement();
}
bankAccount.h:
#ifndef H_bankAccount
#define H_bankAccount
#include <string>
class bankAccount
{
public:
std::string getAcctOwnersName() const;
int getAcctNum() const;
double getBalance() const;
void getMonthlyStatement() const;
void setAcctOwnersName(std::string);
void setAcctNum(int);
void setBalance(double);
virtual void withdraw(double)=0;
virtual void deposit(double)=0;
private:
std::string acctOwnersName;
int acctNum;
double acctBalance;
};
#endif
bankAccount.cpp:
#include "bankAccount.h"
#include <iostream>
using std::string;
string bankAccount::getAcctOwnersName() const
{
return acctOwnersName;
}
int bankAccount::getAcctNum() const
{
return acctNum;
}
double bankAccount::getBalance() const
{
return acctBalance;
}
void bankAccount::setAcctOwnersName(string name)
{
acctOwnersName=name;
}
void bankAccount::setAcctNum(int num)
{
acctNum=num;
}
void bankAccount::setBalance(double b)
{
acctBalance=b;
}
void bankAccount::getMonthlyStatement() const
{
std::cout << "Name on Account: " << getAcctOwnersName() << std::endl;
std::cout << "Account Id: " << getAcctNum() << std::endl;
std::cout << "Balance: " << getBalance() << std::endl;
}
I know this is a lot of code to go through but can anyone help me understand why i cannot create an object from the class serviceChargeChecking the error is telling me that i cannot create an object from the abstract class but it doesn't seem to be abstract to me.
serviceChargeChecking implements void writeCheck(int), but the pure virtual function from checkingAccount has type void writeCheck(), so it's still pure in serviceChargeChecking, which makes the class abstract.
You have this in the abstract class checkingAccount:
virtual void writeCheck()=0;
but implement this in the derived class serviceChargeChecking:
void writeCheck(int);
The signature must be the same.
The writeCheck() method has different signatures in serviceChargeChecking and checkingAccount.
If you use C++11, use override in order to avoid this kind of error.
It's because your CheckingAcount has writeCheck() and serviceChargeChecking has writeCheck(int);
This probably due to the fact that you failed to Override checkingAccount's, writeCheck method, the abstract prototype was was
in checkingAccount class
virtual void writeCheck()=0;
and in serviceChargeChecking class
void writeCheck(int);
note the parameters, you didn't override checkingAccount's writeCheck you probably inherited it (implicitly), the serviceChargeChecking made a new writeCheck with an int parameter.