I made two classes Father and Son, split into .h and .cpp. Why do I get the above Error? I have another Solution with basicly the same includes and no error. Sorry if this is trivial but I'm new to c++ and confused why it works in one Solution but not the other.
Edit: When I run the program, the Terminal still opens and shows the cout lines of the Father, the error seems to happen after that. I know about the soution with including the Father.h inside Son.h. but why doesn't it work the way I wrote it? I like the Idea of including header files inside the cpp files.
Father.h
#pragma once
class Father
{
public:
Father();
~Father();
};
Father.cpp:
#include "Father.h"
#include <iostream>
using namespace std;
Father::Father()
{
cout << "I am the father constructor" << endl;
}
Father::~Father()
{
cout << "I am the father deconstructor" << endl;
}
Son.h:
#pragma once
class Son : public Father
{
public:
void Talk();
};
Son.cpp:
#include "Father.h"
#include "Son.h"
#include <iostream>
using namespace std;
void Son::Talk()
{
cout << "I'am the son" << endl;
}
Main.cpp:
#include "Son.h"
#include <iostream>
using namespace std;
int main()
{
Son Bernd;
}
why doesn't it work the way I wrote it?
Son.cpp compiles fine, because it includes the declaration of Father from Father.h before the declaration of Son from Son.h.
The problem occurs in Main.cpp. Here you only include the declaration of Son from Son.h. The class Father is not known to this compilation unit.
Make sure, that each header includes all of it's dependencies and add
#include "Father.h"
to Son.h.
Related
Firstly, I am giving the codes. Then I am explaining the problem I am facing.
main.cpp
#include <iostream>
#include "acc.h"
using namespace std;
class mem;
int main()
{
show();
return 0;
}
acc.h
#ifndef ACC_H
#define ACC_H
#include "acc.cpp"
void show();
class mem{
int a;
public:
void showa();
void seta(int A);
};
#endif
acc.cpp
#include <iostream>
using namespace std;
void mem::showa(){cout<<a<<endl;}
void mem::seta(int A){a = A;}
void show()
{
mem m;
m.seta(22);
string ss;
cin>>ss;
cout<<"MY name is "<<ss<<" ";
m.showa();
}
"mem" class I declared in "acc.h" file already and added that "acc.h" into acc.cpp file also. But when I am calling that class from a function. It can't response. Showing "a" and "mem" not declared. How can I perfectly link that class definition and member functions of that class so that calling member functions of that class from another function can't create any problem?
If you remove the #include "acc.cpp" from the acc.h file it should compile without any errors. I tried and it compiles for me. I am using Visual Studio 2010 for the same.
Other than this, few more comments:
You can use #pragma once in you header file instead of #ifndef/#define macros. The former is more cleaner.
You dont need to forward declare class mem before main() as you are already including acc.h.
the show() can be moved to where main() is defined making the acc.h/acc.cppfiles dedicated for the mem class.
A header file should always be named after the class it is holding i.e. mem.h/mem.cpp in your case. This informs which file contains which class even without opening the file.
Following this tutorial(https://www.youtube.com/watch?v=gq2Igdc-OSI&index=52&list=PLAE85DE8440AA6B83) I encountered 4 errors on Visual Studio C++ 2017. 3 of them are the same thing and just repeat 'Mother': base class undefined in the daughter.h file. The other error reads:'sayName' is not a member of 'Daughter' Now here is the code. It is quite simple what I want the program to print...I want it to print out two lines of "What are you doing there?" If you could help me with this answer, that would be great. Thank you.
For the Main file
`#include "stdafx.h"
#include
#include"Daughter.h"
#include"Mother.h"
using namespace std;
int main()
{
Mother pot;
pot.sayName();
Daughter kettle;
kettle.sayName();
int pause = 0;
cin >> pause;
}
Mother.h
#ifndef MOTHER_H
#define MOTHER_H
class Mother
{
public:
Mother();
void sayName();
};
#endif
Mother.cpp
#include "stdafx.h"
#include<iostream>
#include"Daughter.h"
#include"Mother.h"
using namespace std;
Mother::Mother()
{
}
void Mother::sayName() {
cout << "What are you doing there?" << endl;
}
Daughter.h
#ifndef DAUGHTER_H
#define DAUGHTER_H
class Daughter:public Mother
{
public:
Daughter();
};
#endif
Daughter.cpp
#include "stdafx.h"
#include<iostream>
#include"Daughter.h"
#include"Mother.h"
using namespace std;
Daughter::Daughter()
{
}
When a class inherits another, it must include the parent class header in its header. In your case, you must add #include "Mother.h" at the top of the daughter header (not only at the .cpp file). The other error is happening because of the first one and correcting it should solve it.
When you write the inheritance syntax class Daughter : public Mother, the Daughter class definition needs to have access to the information about its parent class for several reasons. One of them is the information about inherited methods, which was causing your second error.
Below are by 4 classes, I'm learning about basic c++ syntax and boy is it much harder and less forgiving than other languages I have used. I have a main class, base class "BaseArray" and two sub classes "OrderedArray" and "UnorderedArray".
Main.cpp
#include <iostream>
#include "OrderedArray.cpp"
#include "UnorderedArray.cpp"
using namespace std;
int main() {
system("PAUSE");
return 0;
}
BaseArray.cpp
#include <iostream>
using namespace std;
class BaseArray {
public:
BaseArray::BaseArray() {
}
};
OrderedArray.cpp
#include <iostream>
#include "BaseArray.cpp"
using namespace std;
class OrderedArray : public BaseArray {
OrderedArray::OrderedArray() {
}
};
UnorderedArray.cpp
#include <iostream>
#include "BaseArray.cpp"
using namespace std;
class UnorderedArray : public BaseArray {
UnorderedArray::UnorderedArray() {
}
};
The errors I receive are as followed, from scouting other threads online. I think it might have to do with duplicate calling of classes. To be honest, I have no clue. If someone could point me in the right direction that would be nice, thanks in advance!
error C2011: 'BaseArray':'class' type redefinition
error C2504: 'BaseArray':base class undefined
To fix this error I can remove one of the includes at the top of main.cpp, but I need those to create objects and call functions from the subclasses later on.
You should put your base array in a header:
BaseArray.h
#ifndef BASEARRAY_H_GUARD // include guard
#define BASEARRAY_H_GUARD // include guard
// no using namespace !!
// only includes needed for what's in the header
class BaseArray {
public:
BaseArray();
};
#endif // include guard
And then leave in the cpp only the implementation part of your class:
BaseArray.cpp
#include <iostream>
#include "BaseArray.h"
using namespace std;
BaseArray::BaseArray() { // no need class enclosing: the BaseArray:: prefix is sufficient
}
The you can apply the same principle to the derived classes:
OrderedArray.h
#ifndef BASEARRAY_H_GUARD // include guard
#define BASEARRAY_H_GUARD // include guard
#include "BaseArray.h" // include only those that you need but all those that you need
class OrderedArray : public BaseArray { // requires BaseArray
OrderedArray();
};
#endif
OrderedArray.cpp
#include <iostream> // include headers that are needed for class implementation
#include "OrderedArray.h" // this should be self contained and provide
// evertyhing that is needed for the class itself
using namespace std;
OrderedArray::OrderedArray() {
}
You then have to do the same for UnorderedArray and finally, you have to adapt your main.cpp to include .h instead of .cpp. And you're done.
A final remark: your cpp source code files are now ready for separate compilation. This means that you can no longer compile only main.cpp, hoping that it includes all the code: you have now to compile the 4 cpp files and link them together.
I have two classes - Mother(Base) and Daughter(Derived). I am inheriting a function from Mother class and trying to override in Daughter class. It looks like that it overrides, but my confusion is, even though I don't inherit Mother class, the function still works, so how am I inheriting/overriding it? I am very confused as if I'm really inheriting/overriding anything. Please note in the Derived class that I am not inheriting : public Mother
Thanks for the help, as always!!!
This is my code
Mother.hpp
#ifndef Mother_hpp
#define Mother_hpp
#include <iostream>
#include <string>
class Mother
{
public:
Mother();
void sayName();
};
Mother.cpp
#include <iostream>
#include <string>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
Mother::Mother(){}
void Mother::sayName(){
cout<<"I am Sandy" <<endl;
}
Daughter.hpp
#ifndef Daughter_hpp
#define Daughter_hpp
#include <iostream>
#include "Mother.hpp"
class Daughter : public Mother
{
public:
Daughter();
void sayName();
};
Daughter.cpp
#include <iostream>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
Daughter::Daughter() : Mother(){}
void Daughter::sayName(){
cout << "my name is sarah" <<endl;
}
Main.cpp
#include <iostream>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
Mother mom;
mom.sayName();
Daughter d;
d.sayName();
return 0;
}
but my confusion is, even though I don't inherit Mother class, the function still works, so how am I inheriting/overriding it? I am very confused as if I'm really inheriting/overriding anything.
you don't really override sayName() of your Mother class because (as you said) Daughter class doesn't inherit it in the first place. That is, you need to inherit a class first in order to be able to override its virtual functions.
your second call to sayName() works because it's a call to a member function of Daughter class, which is totally independent from Mother class. Note that, just having multiple independent classes whose member functions share the same signature is not overriding
side note: you shouldn't include Daughter.hpp in Mother.cpp, whether you plan to inherit Mother in Daughter, or not.
I'm noob in C++, and I have the following header file (Header.h)
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
using namespace std;
class Predicate
{
public:
virtual void print() = 0;
};
class UnaryIntegerPredicate : public Predicate
{
public:
virtual void print();
};
class BiaryIntegerPredicate : public Predicate
{
public:
virtual void print();
};
#endif
and in another separate .cpp file (Source.cpp), I tried to implement the print method, but got an "expected an expression" error.
#include "stdafx.h"
#include "Header.h"
#include <iostream>
using namespace std;
UnaryIntegerPredicate::UnaryIntegerPredicate() : Predicate()
{
virtual void print()
{
cout << "int : boolean" << endl;
}
}
what is wrong here, thanks!
I see you are probably coming from a Java background. What you need is
void UnaryIntegerPredicate::print()
{
cout << "int : boolean" << endl;
}
You don't need the stuff surrounding that. Since you have already declared in your header that UnaryIntegerPredicate derives from Predicate, you don't mention that again in the implementation file. You show that the print method that you are writing is the print method of the UnaryIntegerPredicate class by prefixing the name of the method with the name of the class as I have shown above.
You also don't need the virtual keyword in the cpp file because that is already specified in the header file.