I keep getting an error saying:
initializing cannot convert from 'const char *' to 'Address'
I am trying to allow my Person class to use Address as a parameter in the constructor. I'm including my Address header file in the Person header file so I don't know what I'm doing wrong. Also there's nothing in my .cpp file besides calling the default constructor Person myPerson.
Address header file:
#ifndef ADDRESSMODEL
#define ADDRESSMODEL
#define ADDRESSDEBUG
#include <iostream>
#include <string.h>
using namespace std;
class Address {
public:
Address(void);
Address(char* aNumber,
char* aStreetName,
char* aTownName,
char* aCounty);
~Address();
void setAddress(char* aNumber,
char* aStreetName,
char* aTownName,
char* aCounty);
char* getNumber(void);
char* getStreetName(void);
char* getTownName(void);
char* getCounty(void);
protected:
private:
char theNumber[4];
char theStreetName[20];
char theTownName[20];
char theCounty[20];
};
inline Address::Address(void) {
char theNumber[] = "0";
char theStreetName[] = "0";
char theTownName[] = "0";
char theCounty[] = "0";
cout << "\n Default constructor was called" << endl;
}
inline Address::Address(char* aNumber,
char* aStreetName,
char* aTownName,
char* aCounty) {
strcpy(theNumber, aNumber);
strcpy(theStreetName, aStreetName);
strcpy(theTownName, aTownName);
strcpy(theCounty, aCounty);
cout << "\n Regular constructor was called" << endl;
}
inline Address::~Address() {
cout << "\n Deconstructor was called" << endl;
}
#endif // ifndef ADDRESSMODEL
My Person header:
#include "Date.h"
#include <iostream>
#include <string.h>
using namespace std;
class Person {
public:
Person(void);
// Person(Address anAddress);
protected:
private:
// Name theName;
// Date theDate;
Address theAddress;
};
inline Person::Person(void) {
Address theAddress = ("00", "000", "00", "00");
cout << "\n The default constructor was called" << endl;
}
// inline Person :: Person(Address anAddress) {
// cout << "\n The regular constructor was called" << endl;
// }
#endif
First of All re-check if you are including the correct header file in the person Class , I think you have included the "Date.h" which might not be the correct file, You should check it Again.
Then in the constructor of Person class you are re-declaring the theAddress Attribute which is already declared as the private attribute.
it should be something like this:
#include "Date.h"
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
public:
Person(void);
//Person(Address anAddress);
protected:
private:
//Name theName;
//Date theDate;
Address theAddress;
};
inline Person :: Person(void)
{
theAddress = ("00","000","00","00");
cout << "\n The default constructor was called" << endl;
}
you can't call the constructor and re-declare the attribute again.
You should use constructor initializer list instead to call the required constructor of Address class.
Related
I am learning C++ from the book called "Beginning C++ through Game Programming" and this problem showed up. The following code was originally 1 file, but I had to divide it into multiple classes.
Now, I am stuck at inheritance problem. I watched videos, read tutorials about it but nothing seemed to help. The book doesn't help either. I need to add the following attributes:
Use the multiple file class version (I am using it)
From a critter base class -- add at least 2 inheritance critters
The base class should have at least 2 attributes (data)
The inherited class should have at least 2 additional traits different from the base class and the other inherited class
Add the appropriate functions for all the attributes -- both base and inherited
Add appropriate menu items
Any help here would be highly appreciated!
critter.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Critter
{
public:
Critter(const string& name = "");
string GetName() const;
private:
string m_Name;
};
critterImp.cpp
#include <critter.h>
Critter::Critter(const string& name):
m_Name(name)
{}
inline string Critter::GetName() const
{
return m_Name;
}
farm.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Critter;
class Farm
{
public:
Farm(int spaces = 1);
void Add(const Critter& aCritter);
void RollCall() const;
private:
vector<Critter> m_Critters;
};
farmImp.cpp
#include <farm.h>
#include <critter.h>
Farm::Farm(int spaces)
{
m_Critters.reserve(spaces);
}
void Farm::Add(const Critter& aCritter)
{
m_Critters.push_back(aCritter);
}
void Farm::RollCall() const
{
for (vector<Critter>::const_iterator iter = m_Critters.begin();
iter != m_Critters.end();
++iter)
{
cout << iter->GetName() << " here.\n";
}
}
critFarmTest.cpp
#include <farm.h>
#include <critter.h>
int main()
{
Critter crit("Poochie");
cout << "My critter's name is " << crit.GetName() << endl;
cout << "\nCreating critter farm.\n";
Farm myFarm(3);
cout << "\nAdding three critters to the farm.\n";
myFarm.Add(Critter("Moe"));
myFarm.Add(Critter("Larry"));
myFarm.Add(Critter("Curly"));
cout << "\nCalling Roll...\n";
myFarm.RollCall();
return 0;
}
I have created a program in C++ for a class, and one of the requirements is to output a string when certain parts of the program have been called. For most of these I have simply assigned a string to a member variable and then outputted that variable. I wanted to know is it possible for me to assign the string in a destructor and then output that string? When I try it, it outputs nothing.
ie:
Class
private:
string output;
~Class {
output = "destructor has fired!";
}
int main(){
cout << class.message;
}
This is pseudocode so please ignore syntax mistakes/missing pieces.
It certainly is possible to output a message in the destructor, to know that it has fired, and one way to do it is this...
#include <iostream>
#include <string>
using namespace std;
class C{
string output; // by default private
public:
C(){}
~C() { cout << output << endl; }
void setString(const string& s) {
output = s;
}
};
int main()
{
{
C s;
s.setString("Destructor has fired");
}
return 0;
}
If I understand your question right, this is what you are expected to do. Note: no member variable, direct calls to std::cout.
#include <iostream>
#include <string>
using namespace std;
class C{
public:
C() {
cout << "C ctor" << endl;
}
~C() {
cout << "C dtor" << endl;
}
};
int main()
{
{
C s;
}
return 0;
}
I have broken down my issue into a small simple program.
I have a class myclass I have created in a separate .cpp file "classes.cpp" and declared in the header file "classes.h". myclass contains a variable a of which is initialized when instantiated. This makes variable a = 5.
My overall goal is to create a class in a separate .cpp file declared in a .h file which I can create multiple instances of in my main() program. The problem I am having is this.
In my main() function I create an instance of myclass called first.
my main program shows the variable a is set to the number 5.
If I want to change that number using a static function (and it has to be a static function as this relates to something much bigger in another program I am writing). I call the static function directly and in that static_function I create an instance of myclass and call the non_static_function because static functions have no implicit 'this' connecting them to an object.
In my non_static_function I change the value to the number 8. The problem is that the value of variable 'a' in 'first' remains at 5 when I want it to be 8. I need to change the value using first->static_function(8) and not by first->a = 8. . How can I do this?
Code below:
**main.cpp**
#include <iostream>
#include "classes.h"
using namespace std;
int main()
{
myclass *first = new myclass();
cout << "Myclass variable a is = " << first->a << endl;
first->static_function(8); // trying to change myclass variable 'a' to 8.
cout << "But" << endl;
cout << "the actual value of a is still: " << first->a << endl;
}
**classes.h**
#ifndef CLASSES_H_INCLUDED
#define CLASSES_H_INCLUDED
class myclass
{
public:
int a;
myclass();
void non_static_function(int x);
static void static_function(int x);
};
#endif // CLASSES_H_INCLUDED
**classes.cpp**
#include <iostream>
#include <cstdlib>
#include "classes.h"
using namespace std;
myclass::myclass()
{
a = 5;
}
void myclass::non_static_function(int x)
{
a = x;
cout << "The value for variable 'a' was 5 but is now: " << a << endl;
}
void myclass::static_function(int x)
{
myclass *p = new myclass();
p->non_static_function(x);
}
If you want every instance of myclass to have its own a and you want to call a static function to change it then you need to pass the instance you want changed to the static function. A static function can only modify static members of a class or the members of an instance that is inside its scope. Non static member functions can change any variable that is a member of the class.
class Foo
{
private:
int bar;
public:
static void static_function(int value, Foo & foo) { foo.bar = value; }
void non_static_function(int value) { bar = value; }
};
int main()
{
Foo foo;
Foo::static_function(8, foo);
// now bar will have the value of 8
foo.non_static_function(20);
// now bar will have the value of 20
}
I have finally found a way to deal with this small problem. Above the 'myclass' definition in classes.cpp I declare a 'myclass' variable
myclass *tgt; . Then in my constructor for 'myclass' I just allocate the instantiated object to a my global myclass variable of which I can access from the myclass definition tgt = this; Now I can use tgt in my static function to call the non_static_function in my 'myclass' definition and it all works perfectly.
NathanOliver, you are correct in saying that I need a class instance but the way I have done it here suits my needs. Passing the instance of myclass is certainly another way of doing this but it would require a global function above my 'myclass' definition.
Thanks for the help.
**main.cpp**
#include <iostream>
#include "classes.h"
using namespace std;
int main()
{
myclass *first = new myclass();
cout << "Myclass variable a is = " << first->a << endl;
first->non_static_function(8); // trying to change myclass variable 'a' to 8.
cout << "But" << endl;
cout << "The actual value of a is still: " << first->a << endl;
myclass *second = new myclass();
cout << "For the 'second' class the variable a is: " << second->a << endl;
second->non_static_function(23);
cout << "After calling the static function from 'second' the value of a is: " << second->a << endl;
cout << "And first->a is still: " << first->a << endl;
}
**classes.h**
#ifndef CLASSES_H_INCLUDED
#define CLASSES_H_INCLUDED
class myclass
{
public:
int a;
myclass();
void non_static_function(int x);
static void static_function(int x);
};
#endif // CLASSES_H_INCLUDED
**classes.cpp**
#include <iostream>
#include <cstdlib>
#include "classes.h"
using namespace std;
myclass *tgt; // *Add a global myclass variable above the myclass
definition*
myclass::myclass()
{
tgt = this; // *In the constructor allocate the instantiated class
//from main() to "tgt" .*
a = 5;
}
void myclass::non_static_function(int x)
{
a = x;
// Now see that the value of a is changed.
cout << "The value for variable 'a' was 5 but is now: "<< this->a << endl;
}
void myclass::static_function(int x)
{
tgt->non_static_function(x);
}
I'm studying for pointer and inheritance of C++.\
I made pointer of vector Employee class which has name and salary in it.
Also, it has print function which printout name and salary.
I made it virtual
virtual void print() const;
Also here's the implementation of function
void Employee::print() const
{
cout << "Inquiry Employee info..." << endl;
cout << "Name:" << get_name() << "\n";
cout << "Salary:" << get_salary() << "\n" << "\n";
}
In derived class, I added one more private data Department Name and override print function.
I put derived class in pointer vector, and tried to call the function on derived function.
However, it only calls the function in base class.
When I make derived class object itself and called that print function, it worked.
What should I change to make derived class object in base class pointer vector can call function in derived class?
Addition ##
#ifndef MANAGER_H
#define MANAGER_H
#include <string>
#include<iostream>
#include "ccc_empl.h"
#include <iomanip>
class Manager : public Employee
{
public:
Manager();
Manager(string name, double salary, string dept);
~Manager();
virtual string get_department() const;
void print();
private:
string deptName;
};
#endif
implementation
#include <iostream>
#include <string>
#include "manager.h"
#include "ccc_empl.h"
Manager::Manager(){}
Manager::Manager(string name, double salary, string dept)
: Employee(name,salary)
{
deptName = dept;
}
Manager::~Manager(){}
string Manager::get_department() const
{
return deptName;
}
void Manager::print()
{
cout << "Inquiry Manager info..." << endl;
cout << "Name:" << get_name() << "\n";
cout << "Salary:";
cout << get_salary() << "\n";
cout << "Department:"<< get_department() << endl << endl;
}
You haven't shown us the Derived class print function, but I would guess that you forgot to declare it const. Easy mistake to make.
I'm working on a homework assignment for my C++ class and have ran across a problem that I cannot figure out what I am doing wrong.
Just to note, the separation of the files is necessary and I realize this would be much easier if I just made a structure AttackStyles inside the main and forgo the additional class file altogether.
The base of my problem is that I cannot seem to be able to loop through an array of classes and pull out base data. Here is the code:
// AttackStyles.h
#ifndef ATTACKSTYLES_H
#define ATTACKSTYLES_H
#include <iostream>
#include <string>
using namespace std;
class AttackStyles
{
private:
int styleId;
string styleName;
public:
// Constructors
AttackStyles(); // default
AttackStyles(int, string);
// Destructor
~AttackStyles();
// Mutators
void setStyleId(int);
void setStyleName(string);
// Accessors
int getStyleId();
string getStyleName();
// Functions
};
#endif
/////////////////////////////////////////////////////////
// AttackStyles.cpp
#include <iostream>
#include <string>
#include "AttackStyles.h"
using namespace std;
// Default Constructor
AttackStyles::AttackStyles()
{}
// Overloaded Constructor
AttackStyles::AttackStyles(int i, string n)
{
setStyleId(i);
setStyleName(n);
}
// Destructor
AttackStyles::~AttackStyles()
{}
// Mutator
void AttackStyles::setStyleId(int i)
{
styleId = i;
}
void AttackStyles::setStyleName(string n)
{
styleName = n;
}
// Accessors
int AttackStyles::getStyleId()
{
return styleId;
}
string AttackStyles::getStyleName()
{
return styleName;
}
//////////////////////////////////////////////
// main.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include "attackStyles.h"
using namespace std;
int main()
{
const int STYLE_COUNT = 3;
AttackStyles asa[STYLE_COUNT] = {AttackStyles(1, "First"),
AttackStyles(2, "Second"),
AttackStyles(3, "Third")};
// Pointer for the array
AttackStyles *ptrAsa = asa;
for (int i = 0; i <= 2; i++)
{
cout << "Style Id:\t" << ptrAsa->getStyleId << endl;
cout << "Style Name:\t" << ptrAsa->getStyleName << endl;
ptrAsa++;
}
system("PAUSE");
return EXIT_SUCCESS;
}
My question is why do I get the error:
"a pointer to a bound function may only be used to call the function"
on both ptrAsa->getStyleId and ptrAsa->getStyleName?
I cannot figure out what is wrong with this!
You are missing () around the function calls. It should be ptrAsa->getStyleId().
You are missing parenthesis on both calls, it should be
ptrAsa->getStyleId()
to call the function.
ptrAsa->getStyleId
is used to refer to a member value / attribute.
You need to invoke the function, not merely reference it:
std::cout << "Style Id:\t" << ptrAsa->getStyleId() << "\n";
std::cout << "Style Name:\t" << ptrAsa->getStyleName() << "\n";
You are Forgot to put () in last in Your Function(ptrAsa->getStyleId ) Calling with arrow operator.