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.
Related
I am using c++11 compiler.
I have two classes - class Test and class TestHelper.
The class Test is a friend-to-class TestHelper.
The class Test is only which we can access from outside.
Now, we want to call Test API i.e. setVal(). This setVal() should call
Test2 API i.e. setX and is expecting this pointer. I don't want to use this pointer but want
to use a smart pointer instead. How can I do so?
The notion of this kind of desirability is because of the fact that in reality, my class Test is pretty big. So, I am trying to make a helper class for Test i.e.
class TestHelper;
class Test
{
friend class TestHelper;
int x;
public:
void display() {
std::cout << x;
}
void setVal(int val) {
TestHelper testH;
testH.setX(this, 324);
}
};
class TestHelper
{
public:
void setX(Test *test, int val) {
/** some algorithm here and then change val to something else */
test->x = val*100;
}
};
int main()
{
std::cout << "Hello World!\n";
Test x;
x.setVal(130);
}
I tried changing the prototype from void setX(Test *test, int val)
to void setX(std::shared_ptr<Test> test, int val) but don't know how to pass this pointer
as std::shared_ptr<Test> test here.
So here is working solution with shared pointers. The example doesn't even compile due to missing definitions so you have to restructure your code into headers and cpp files.
Test.h:
#ifndef TEST_H
#define TEST_H
#include <memory>
#include "TestHelper.h"
class Test : public std::enable_shared_from_this<Test>
{
private:
friend class TestHelper;
int x;
public:
void display();
void setVal(int val);
};
#endif
Test.cpp:
#include <iostream>
#include "Test.h"
void Test::display() {
std::cout << x;
}
void Test::setVal(int val) {
TestHelper testH;
testH.setX(shared_from_this(), 324);
}
TestHelper.h:
#ifndef TESTHELPER_H
#define TESTHELPER_H
class Test;
class TestHelper
{
public:
void setX(std::shared_ptr<Test> test, int val);
};
#endif
TestHelper.cpp:
#include <memory>
#include "TestHelper.h"
#include "Test.h"
void TestHelper::setX(std::shared_ptr<Test> test, int val) {
/** some algorithm here and then change val to something else */
test->x = val*100;
}
main.cpp:
#include <iostream>
#include <memory>
#include "Test.h"
int main(void){
std::cout << "Hello World!\n";
auto x = std::make_shared<Test>();
x->setVal(130);
x->display();
}
You can run it here: https://paiza.io/projects/e/79dehCx0RRAG4so-sVZcQw
I don't understand why you want this, here's a few variants that compile
reference
// Reference variant
#include <iostream>
class Test;
class TestHelper
{
public:
void setX(Test & test, int val);
};
class Test
{
friend class TestHelper;
int x;
public:
void display() {
std::cout << x;
}
void setVal(int val) {
TestHelper testH;
testH.setX(*this, 324);
}
};
void TestHelper::setX(Test &test, int val)
{
/** some algorithm here and then change val to something else */
test.x = val*100;
}
int main()
{
Test x;
x.setVal(130);
x.display();
}
http://cpp.sh/7t3ec
shared ptr
// Shared ptr variant
#include <iostream>
#include <memory> // Required for shared_ptrs
class Test;
class TestHelper
{
public:
void setX(std::shared_ptr<Test> test, int val);
};
class Test : public std::enable_shared_from_this<Test>
{
friend class TestHelper;
int x;
public:
void display() {
std::cout << x;
}
void setVal(int val) {
TestHelper testH;
testH.setX(shared_from_this(), 324);
}
};
void TestHelper::setX(std::shared_ptr<Test> test, int val)
{
/** some algorithm here and then change val to something else */
test->x = val*100;
}
int main()
{
auto x = std::make_shared<Test>(); // x needs to be created as shared_ptr or it won't work
x->setVal(130);
x->display();
}
http://cpp.sh/87ao2
Perhaps with these you can refine your question?
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
Like title say, I think i have a problem on one of my include.
I'm studient, my teacher gave me a solution to create a visual interface (coded by himself) named EZ-Draw.
But somewhere in my code there is a problem 'cause my compiler tell me many errors of this style:
|364|undefined reference to `CreateCompatibleDC#4'|
|559|undefined reference to `SelectObject#8'|
...
my code:
interpreteur.hpp
#ifndef INTERPRETEUR_HPP_INCLUDED
#define INTERPRETEUR_HPP_INCLUDED
#include <sstream>
#include <map>
#include <math.h>
#include "Pile_Template.hpp"
#include "ez-draw++.h"
#define PI 3.14159265
class Interpreteur {
private:
void (Interpreteur::*ptr)();
EZWindow myWindow;
Pile<double> pile;
Pile<string> pilestr;
bool run;
public:
map<string,void(Interpreteur::*)()> myMap;
Interpreteur();
~Interpreteur();
inline bool getRun() {return run;};
inline void setEmpilerPile(double nombre) {pile.empiler(nombre);};
template <typename T>
string itos(T nombre) // convertit un double en string
{
ostringstream ss;
ss<<nombre;
return ss.str();
}
void addition();
void moins();
void multiplie();
void divise();
void quit();
void push();
void pushstr();
void pop();
void popstr();
void copy();
void copystr();
void print();
void printstr();
void display();
void displaystr();
void count();
void countstr();
void swap();
void swapstr();
void sinus();
void cosinus();
void tangente();
void racine();
void trunc();
void line();
void color();
void drawstr();
void triangle();
void rectangle();
void circle();
};
#endif // SOUS_PROGRAMMES_HPP_INCLUDED
interpreteur.cpp
#include "interpreteur.hpp"
#include <sstream>
#include <map>
#include <math.h>
#include <string>
using namespace std;
void Interpreteur::addition()
{
pile.empiler(pile.depiler()+pile.depiler());
}
void Interpreteur::moins()
{
double nombre=pile.depiler();
nombre=pile.depiler()-nombre;
pile.empiler(nombre);
}
void Interpreteur::multiplie()
{
pile.empiler(pile.depiler()*pile.depiler());
}
void Interpreteur::divise()
{
double nombre=pile.depiler();
nombre=pile.depiler()/nombre;
pile.empiler(nombre);
}
void Interpreteur::quit()
{
run=false;
}
void Interpreteur::push()
{
double i;
cin>>i;
pile.empiler(i);
}
void Interpreteur::pushstr()
{
string chaine;
char merde;
cin>>merde;
if(merde=='"')
{
getline(cin,chaine,'"');
pilestr.empiler(chaine);
}
else
{
cin.putback(merde);
cerr<<"mauvaise chaine de caractères"<<endl;
}
}
void Interpreteur::pop()
{
pile.depiler();
}
void Interpreteur::popstr()
{
pilestr.depiler();
}
void Interpreteur::copy()
{
int i=pile.depiler();
pile.empiler(pile[pile.getSommet()-i]);
}
void Interpreteur::copystr()
{
int i=pile.depiler();
pilestr.empiler(pilestr[pile.getSommet()-i]);
}
void Interpreteur::print()
{
cout<<pile.depiler()<<endl;
}
void Interpreteur::printstr()
{
cout<<pilestr.depiler()<<endl;
}
void Interpreteur::display()
{
pile.afficher(cout);
}
void Interpreteur::displaystr()
{
pilestr.afficher(cout);
}
void Interpreteur::count()
{
pile.empiler(pile.getSommet());
}
void Interpreteur::countstr()
{
pilestr.empiler(itos(pilestr.getSommet()));
}
void Interpreteur::swap()
{
double first=pile.depiler();
double second=pile.depiler();
pile.empiler(first);
pile.empiler(second);
}
void Interpreteur::swapstr()
{
string first=pilestr.depiler();
string second=pilestr.depiler();
pilestr.empiler(first);
pilestr.empiler(second);
}
void Interpreteur::sinus()
{
pile.empiler(sin(pile.depiler()*PI/180));
}
void Interpreteur::cosinus()
{
pile.empiler(cos(pile.depiler()*PI/180));
}
void Interpreteur::tangente()
{
pile.empiler(tan(pile.depiler()*PI/180));
}
void Interpreteur::racine()
{
pile.empiler(sqrt(pile.depiler()));
}
void Interpreteur::trunc()
{
int x=pile.depiler();
pile.empiler(x);
}
void Interpreteur::line()
{
int y2=pile.depiler();
int x2=pile.depiler();
int y1=pile.depiler();
int x1=pile.depiler();
myWindow.drawLine(x1,y1,x2,y2);
}
void Interpreteur::color()
{
int couleur=pile.depiler();
switch(couleur)
{
case 1:{myWindow.setColor(ez_black);break;}
case 2:{myWindow.setColor(ez_red);break;}
case 3:{myWindow.setColor(ez_green);break;}
case 4:{myWindow.setColor(ez_blue);break;}
case 5:{myWindow.setColor(ez_cyan);break;}
case 6:{myWindow.setColor(ez_magenta);break;}
case 7:{myWindow.setColor(ez_yellow);break;}
//pourquoi que on a pas fait le gris ? ez_grey
default:{pile.empiler(couleur); cerr<<"couleur inconnue"<<endl; break;}
}
// COULEUR : ez_black, ez_white, ez_grey, ez_red, ez_green, ez_blue,ez_yellow, ez_cyan, ez_magenta
}
void Interpreteur::drawstr()
{
string str=pilestr.depiler();
int y1=pile.depiler();
int x1=pile.depiler();
myWindow.drawText(EZ_MC,x1,y1,str);
}
void Interpreteur::triangle()
{
int y3=pile.depiler();
int x3=pile.depiler();
int y2=pile.depiler();
int x2=pile.depiler();
int y1=pile.depiler();
int x1=pile.depiler();
myWindow.drawTriangle(x1,y1,x2,y2,x3,y3);
}
void Interpreteur::rectangle()
{
int y2=pile.depiler();
int x2=pile.depiler();
int y1=pile.depiler();
int x1=pile.depiler();
myWindow.drawRectangle(x1,y1,x2,y2);
}
void Interpreteur::circle()
{
int y2=pile.depiler();
int x2=pile.depiler();
int y1=pile.depiler();
int x1=pile.depiler();
myWindow.drawCircle(x1,y1,x2,y2);
}
Interpreteur::Interpreteur()
{
run=true;
myMap["+"]=&Interpreteur::addition;
myMap["-"]=&Interpreteur::moins;
myMap["*"]=&Interpreteur::multiplie;
myMap["/"]=&Interpreteur::divise;
myMap["exit"]=&Interpreteur::quit;
myMap["push"]=&Interpreteur::push;
myMap["pushstr"]=&Interpreteur::pushstr;
myMap["pop"]=&Interpreteur::pop;
myMap["popstr"]=&Interpreteur::popstr;
myMap["copy"]=&Interpreteur::copy;
myMap["copystr"]=&Interpreteur::copystr;
myMap["print"]=&Interpreteur::print;
myMap["printstr"]=&Interpreteur::printstr;
myMap["display"]=&Interpreteur::display;
myMap["displaystr"]=&Interpreteur::displaystr;
myMap["count"]=&Interpreteur::count;
myMap["countstr"]=&Interpreteur::countstr;
myMap["swap"]=&Interpreteur::swap;
myMap["swapstr"]=&Interpreteur::swapstr;
myMap["sin"]=&Interpreteur::sinus;
myMap["cos"]=&Interpreteur::cosinus;
myMap["tan"]=&Interpreteur::tangente;
myMap["sqrt"]=&Interpreteur::racine;
myMap["trunc"]=&Interpreteur::trunc;
myMap["line"]=&Interpreteur::line;
myMap["color"]=&Interpreteur::color;
myMap["drawstr"]=&Interpreteur::drawstr;
myMap["triangle"]=&Interpreteur::triangle;
myMap["rectangle"]=&Interpreteur::rectangle;
myMap["circle"]=&Interpreteur::circle;
}
Interpreteur::~Interpreteur()
{
map<string, void (Interpreteur::*)()>::iterator it;
myMap.erase(it);
}
and here the "EZ-Draw documentation" gived by my teacher to understand ez-draw and ez-draw++
I don't understand what the compiler is trying to tell me
You are using C functions from C++, for this to work you need to explicitly tell the compiler those are C functions. The C header file you are using from C++ should contain those lines to be usable in C++:
#ifdef __cplusplus
extern "C" {
#endif
//The header file which declares the functions which are not linked correctly here
#ifdef __cplusplus
}
#endif
A bit of explanation:
The function "name mangling" is different for C and C++, since in C++ you can overload a function the name of the function alone does not identify a function uniquely, so the compiler adds some symbols to the names of functions in background when you compile to make those names unique (that's the DC#4 in CreateCompatibleDC#4).
Since your linker expects C++ functions, it searches for CreateCompatibleDC#4, but some of your files get compiled in C, and export a function named CreateCompatible, that's why you get a "undefined reference": the linker is telling you it can not find the definition of some functions.
My question is very beginner, and yes I have looked it up extensively, but when I do the things I've found online Xcode gives me errors.
Basically, I'm just curious how to implement a constructor for a derived class. My class is called "Sensor" and the derived classes are digitalSensor and analogSensor.
Here's my sensor.h:
#ifndef __Program_6__sensor__
#define __Program_6__sensor__
#include <iostream>
class sensor {
char* SensorName;
float energyDraw;
int functioning;
int onoff;
public:
sensor(char*n, float pc);
virtual void print();
void setOK(int K);
int getOK();
void setOnOff(int n);
int getOnOff();
};
//---------
class digitalSensor : public sensor {
int reading;
public:
digitalSensor(char*n, float pc);
virtual void print();
void setCurrentReading(int r);
int getCurrentReading();
};
class analogSensor : public sensor {
int Reading;
int minRead;
int maxRead;
public:
analogSensor(char *n, float pc, int mm, int mx);
virtual void print();
void setCurrentReading(int r);
int getCurrentReading();
};
#endif /* defined(__Program_6__sensor__) */
And here's my sensor.cpp, you can see the beginnings of my digitalSensor work at the bottom.
#include "sensor.h"
#include "definitions.h"
using namespace std;
//--------SENSOR CLASS------------//
sensor::sensor(char *n, float pc) {
SensorName = (char*)malloc(strlen(n)+1);
energyDraw = pc;
functioning = WORKING;
onoff = OFF;
}
void sensor::print() {
cout << " Sensor: " << SensorName;
cout << " Power Consumption: " << energyDraw;
if (functioning == WORKING) {
cout << "\nSensor is functioning correctly\n";
if (onoff == ON) {
cout << "Sensor is On";
}
if (onoff == OFF) {
cout << "Sensor is Off";
}
}
if (functioning == NOTWORKING) {
cout << "Sensor is not functioning correctly";
}
}
void sensor::setOK(int k) {
functioning = k;
}
int sensor::getOK() {
return functioning;
}
void sensor::setOnOff(int n) {
onoff = n;
}
int sensor::getOnOff() {
return onoff;
}
//---------------------------------//
//*********DIGITAL SENSOR**********//
sensor digitalSensor::digitalSensor(char *n, float pc) {
}
In a nutshell: I need to make a constructor function for the digital sensor class. What am I missing, how do I do that? Thanks in advance for any help or knowledge on this!
Implement its constructor like this:
digitalSensor::digitalSensor(char*n, float pc) : sensor(n, pc)
{
}
As you can see, it doesn't return anything and it calls its parent's constructor.
But you're doing this which is wrong:
sensor digitalSensor::digitalSensor(char *n, float pc) {
^^^^^^ constructors shall not return anything
}
You should call your base class constructor at the derived class's member initialization list.
For example:
class digitalSensor : public sensor {
int reading;
public:
digitalSensor(char*n, float pc): sensor(n, pc), reading(0){}
virtual void print();
void setCurrentReading(int r);
int getCurrentReading();
};
This defines the digitalSensor constructor inline. You can also define it with scope resolution operator outside class:
digitalSensor::digitalSensor(char*n, float pc):sensor(n, pc), reading(0){}
//^^Note that constructor of a class does not have any return type
It really depends on how your base class constructors are provided. But this could be one way to do it. You may find Initializing base classes and members useful.
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.....