"Base class undefined." - c++

I've got an error:
error C2504: 'employee' : base class undefined.
I'm using Visual Studio 2010.
This is my first time working with inheritance in C++ and I can't figure out what I'm doing wrong. When I try to make a class that derives another, it says that the parent class is undefined, even though I have the header file of the parent class included. What could be the problem?
Main.cpp:
#include <string>
#include <iostream>
using namespace std;
#include "manager.h"
int main()
{
manager ian;
ian.getdata();
ian.putdata();
return 0;
}
Manager.h:
#pragma once
#include "employee2.h"
#include "student.h"
class manager : private employee2, private student //Error happens here
{
//Stuff
};
Student.h:
#pragma once
class student
{
//Stuff
};
Employee2.h:
#pragma once
#include "employee.h"
class employee2 : employee
{
//Stuff
};
It says that both the student class and the employee2 class are undefined. Also, the employee2 class inherits a class called employee, which also gets the same error. What am I doing wrong?
EDIT: Here's my student.cpp file. All the other .cpp files that I'm getting errors with look similar to this one.
#include "student.h"
void student::getedu(){
cout << "Enter name of school or university: ";
cin >> school;
cout << "Enter highest degree earned \n";
cout << "(Highschool, Bachelor's Master's, PhD)";
cin >> degree;
}
void student::putedu() const{
cout << "\n School or university: " << school;
cout << "\n Highest degree earned: " << degree;
}

I don't believe you are getting an "x is undefinied" based upon the code you showed. Actually, it appears you are using #include wrong.
#include employee.h
should be
#include "employee.h"
Other than that, other than the missing employee class you mention, and manager not having getdata and "putdata in your example, you code will compile fine.

Make sure, there are empty spaces at the end of each file.
Before compilation process the files are concatinated and your pragma could be deactivated because of sth like this:
}#pragma once
the '}' comes from the previously included file.
That could be a reason. Besides, the code looks, like it should be compilable.

Related

How to define class constructor outside class in another file?

So, I've got a class Car:
car.h
#ifndef CAR_H
#define CAR_H
#include <iostream>
#include <string.h>
#include "car.cpp"
// Car class with its attributes
class Car {
public:
std::string brand;
std::string model;
int year;
// Constructor
Car(int year, std::string model, std::string brand);
};
#endif
and I wanted to make a class constructor definition outside the class in another .cpp file:
car.cpp
#include <string.h>
Car::Car(int year, std::string model, std::string brand)
{
this->brand = brand;
this->model = model;
this->year = year;
}
I tried to compile, but this error has occurred:
car.cpp:3:1: error: ‘Car’ does not name a type
Why it happened and how to fix it?
My main.cpp:
#include <iostream>
#include "car.h"
using namespace std;
int main() {
// Create an object of Car
Car carObj1 = Car(1992, "model X", "Brand1");
// Create another object of Car
Car carObj2 = Car(2003, "model Y", "Brand2");
// Print attribute values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
You got the includes the wrong way round. car.cpp should #include "car.h" not the other way around.
Also the correct header file for std::string is <string> not <string.h>
Also member initialisation is better done with initialiser lists not assignment
Car::Car(int year, std::string model, std::string brand) :
brand(brand), model(model), year(year)
{
}
#include "car.cpp"
This is wrong. Never include source files.
‘Car’ does not name a type
Why it happened
car.cpp attempts to use the class Car which has not been defined.
how to fix it?
Add #include "car.h" into car.cpp to define Car before its use. Then remove #include "car.cpp" from car.h to avoid recursive inclusion that would prevent correct order of inclusion.

Error: "x" was not declared on this scope when using class files

I'm having a problem when using the class files and the compiler error says "error: 'x' was not declared on this code" while it points out the cout, string, and endl. I have already wrote "#include " and "#include " in both header, class, and main file.
(Sorry for my English)
I'm just a beginner and I wanted to know the basics
Added #include and #include in both files
//Main File (main.cpp)
#include <iostream>
#include "test.h"
#include <string>
using namespace std;
int main()
{
test *person = new person("Phroton",14)
person.Display();
return 0;
}
//test.h
#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <string>
class test
{
private:
string name;
int age;
public:
void Display(){
cout << "I'm " << name << " and I'm " << age << "years old" << endl;
}
};
#endif // TEST_H
//test.cpp (There is no problem with this file at all)
#include "test.h"
#include <iostream>
#include <string>
test::test(string iname, int iage)
{
name = new string;
age = new int;
*name = iname;
*age = iage;
}
test::~test()
{
delete name;
delete age;
cout << "Info Deleted" << endl;
}
Answering the specific problem you have asked:
This is because you have not specified the namespace cout and endl belong to in the file test.h.
The statement in Display should be:
std::cout << "I'm " << name << " and I'm " << age << "years old" << std::endl;
The alternative to this is the using namespace std declaration but this is considered a bad practice (especially in a header file).
Note:
You do not need using namespace std in main.cpp as you are not using any functions from the std namespace there. Even if you do, use the std::name instead of the using declaration.
Member function definitions are usually present in .cpp files. So you can define the function Display to test.cpp.
Also consider moving away from raw pointers to smart pointers.

prototype for "...." does not match any in class "..."

I started to pick up some c++ programming and i am learning the concept of classes and objects. So I looked on the web for some exercises I can practice on. I have read that it is good practice to have the main, header and constructor file separated and not into one long file.
I'm trying to break the following code into three separate files :
// Exercises: Classes
// Exercise 3
// Exercises: Classes
// Exercise 3
#include <iostream>
using namespace std;
class Student{
public:
char *name;
int mark1;int mark2;
Student(char* na, int ma1,int ma2){
name=na;mark1=ma1;mark2=ma2;
}
int calc_media(){return (mark1+mark2)/2;}
void disp(){
cout << "Student:" << name << " \n media:"<< calc_media() <<"\n";
}
};
int main(){
char* nam;int m1,m2;
cout << "Enter name:";
cin>> nam;
cout << "Enter marks of two subjects:";
cin>> m1;
cin>> m2;
Student student1(nam,m1,m2);
student1.disp();
return 0;
}
into the following files:
main.cpp:
#include <iostream>
#include <string>
#include "student_example.h"
using namespace std;
int main()
{
int marc1,marc2;
char nam;
cout<<"Please enter the name of the student: ";
cin>>nam;
cout<<"Please enter the two grades of the student"<<"\n grade one:";
cin>>marc1;
cout<<"Grade two";
cin>>marc2;
student_Example student1;
student1.disp();
return 0;
}
header file (student_Example.h)
#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>
class student_Example
{
public:
char name;
int mark1, mark2;
int calc_media(){
return (mark1+mark2/2);
}
void disp(){
std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
}
};
And constructor:
#include <iostream>
#include <string>
#include "student_Example.h"
student_Example::student_Example(char nam, int marc1, int marc2)
{
name=nam;
mark1=marc1;
mark2=maec2;
}
Im getting the error
"error: prototype for **'student_Example::student_Example(char, int, int)' does not match any class 'student_Example'**
Any advice what may be going on here? Thanks in advance :)
Your header file with class student_Example doesn't promise a constructor. (And seems to be missing and #endif)
#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>
class student_Example
{
public:
student_Example(char nam, int marc1, int marc2); //<-- as pointed out in the error
char name;
int mark1, mark2;
int calc_media(){
return (mark1+mark2/2);
}
void disp(){
std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
}
};
#endif //<-- this too
While we are there we can use an member initialiser list in the constructor
student_Example::student_Example(char nam, int marc1, int marc2) :
name(nam),
mark1(marc1),
mark2(marc2) //assume maerc2 was a typo
{
}
Edit:
Note that student_Example(char nam, int marc1, int marc2) is a declaration that you will define a constructor taking a char and two ints., which you have done in your cpp file.
You can make an object like this
student_Example example('n', 1, 2);
Without this non-default constructor, a default constructor taking no parameters would have been automatically generator for you, so you could have made an object like this:
student_Example example;
Now you have defined a constructor that will no longer happen. You either need to add this to your class, or make sure you use the constructor taking parameters.
doctorlove fixes your issue, but is also good practice to have the methods in the .cpp file, like this:
student_Example.h
#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>
class student_Example
{
public:
student_Example(char nam, int marc1, int marc2); //<-- as pointed out in the error
char name;
int mark1, mark2;
int calc_media();
void disp();
};
#endif
student_Example.cpp
#include "student_Example.h"
student_Example::student_Example(char nam, int marc1, int marc2) :
name(nam),
mark1(marc1),
mark2(marc2) //assume maerc2 was a typo
{
}
int student_Example::calc_media(){
return (mark1+mark2/2);
}
void student_Example::disp(){
std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
}

I can't get second class in C++ Code::Blocks to work

I just started learning C++ and currently I'm trying to create new namespaces in 2 different classes.
But I can't seem to add second class to my project in CodeBlocks, even though I think I included everything properly, it just looks like compiler ignores the second class and cant import the namespace I created.
Here is the code:
#include <iostream>
#include "Pokemon.h"
#include "animals.h"
using namespace std;
using namespace pokemons;
int main()
{
Pokemon pikachu("Pikachu", 1);
pikachu.pokeAttack();
return 0;
}
Source file:
#include "animals.h"
#include <iostream>
using namespace std;
namespace pokemons{
Pokemon::Pokemon()
{
cout << "I choose you, " << name << endl;
}
Pokemon::~Pokemon()
{
cout << "Come back to Pokeball, " << name << endl;
}
Pokemon::Pokemon(string name, int type){
this->name = name;
this->type = type;
cout << "I choose you, " << name << "!!!" << endl;
}
void Pokemon::pokeAttack(){
if (type = 1){
cout << name << " used tackle" << endl;
}
}
}
Header file:
#ifndef POKEMON_H
#define POKEMON_H
#include <string>
namespace pokemons{
class Pokemon
{
public:
Pokemon();
virtual ~Pokemon();
void pokeAttack();
Pokemon(std::string name, int type);
protected:
private:
std::string name;
int type;
};
}
#endif // POKEMON_H
The first class and namespace works perfectly fine so I'm not including it here. Don't mind the pokemon, I just didn't know what to use for training.
Oh, and here is the error http://prntscr.com/aw12nj
||=== Build: Debug in namespacesTrening (compiler: GNU GCC Compiler) ===|
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|7|error: 'pokemons' is not a namespace-name|
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|7|error: expected namespace-name before ';' token|
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp||In function 'int main()':|
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|12|error: 'Pokemon' was not declared in this scope|
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|12|note: suggested alternative:|
include\Pokemon.h|9|note: 'poki2::Pokemon'|
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|12|error: expected ';' before 'pikachu'|
C:\Users\PC\Documents\Ceplusplus\namespacesTrening\main.cpp|14|error: 'pikachu' was not declared in this scope|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Adding the first class:
source file Pokemon.cpp
#include "Pokemon.h"
#include <iostream>
namespace poki2{
Pokemon::Pokemon()
{
cout << "I choose you, " << name << endl;
}
Pokemon::~Pokemon()
{
cout << "Come back to Pokeball, " << name << endl;
}
Pokemon::Pokemon(string name, int type){
this->name = name;
this->type = type;
cout << "I choose you, " << name << "!!!" << endl;
}
void Pokemon::pokeAttack(){
if (type = 1){
cout << name << " used thunderbolt!!!" << endl;
}
}
}
The header file Pokemon.h
#ifndef POKEMON_H
#define POKEMON_H
#include <iostream>
using namespace std;
namespace poki2 {
class Pokemon
{
public:
Pokemon();
virtual ~Pokemon();
Pokemon(string name, int type);
void pokeAttack();
protected:
private:
string name;
int type;
};
}
#endif // POKEMON_H
You have Pokemon class in both namespaces, so you need to make it clear for the compiler which namespace you are referring to.
pokemons::Pokemon
poki2::Pokemon
And you use also the same macro name for both animals.h and Pokemon.h
#ifndef POKEMON_H
#define POKEMON_H
[...]
#endif
In this case, the Pokemon.h will be included first and the POKEMON_H macro will be defined, so when the animals.h is included, everything between #ifndef POKEMON_H and #endif will be dropped.
#include "Pokemon.h"
#include "animals.h"
When it comes to you main.cpp file, you have included both header files, but you are only using pokemons namespace.
#include <iostream>
#include "Pokemon.h"
#include "animals.h"
using namespace std;
using namespace pokemons; // You have selected pokemons namespace.
int main()
{
Pokemon pikachu("Pikachu", 1);
pikachu.pokeAttack();
return 0;
}
If you want to use both namespaces, the best way is to spesify it in the declaration of the variable and drop using namespace
pokemons::Pokemon pikachu("Pikachu", 1); // Instance of Pokemon class in namespace pokemons
poki2::Pokemon pikachu2("Pikachu", 1) // Instance of Pokemons class in namespace poki2

class undefined C++

In test i am trying to create object partTimeEmployee, but i get an undefined reference error, and i don't understand why? I dont understand the error because i pass it the right kind of data. is my .cpp and .h not included right?
test.cpp: (.text+0xb7): undefined reference to 'partTimeEmployee(std::basic_string, std::allocator >)'
work.h
#ifndef WORK_H
#define WORK_H
using namespace std;
#include <string>
class work{
public:
void DisplayMe();
work(string x,string y);
work();
~work();
string name;
string id;
};
#endif
work.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib.h>
#include "work.h"
using namespace std;
work::work(){
cout<<name<<"in default work constructor"<<endl;
}
work::~work(){
cout<< name << " calling work destructor. " << endl;
}
work::work(string x, string y){
name = x;
id = y;
cout << "in parent constructor" <<endl;
}
void work::DisplayMe(){
cout << "NAME: " << name << " ID#: " << id <<endl;
}
Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
include "work.h"
using namespace std;
class Employee : public sfasu{
public:
Employee();
Employee(string x);
~Employee();
string department;
};
#endif
Employee.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib.h>
#include "Employee.h"
using namespace std;
Employee::Employee(){
cout<<name<<"in default sfasu constructor"<<endl;
}
Employee::~Employee(){
cout<< name << " calling parent destructor. " << endl;
}
Employee::Employee(string x){
department = x;
cout << "in parent constructor" <<endl;
}
partTimeEmployee.h
#ifndef PARTTIMEEMPLOYEE_H
#define PARTTIMEEMPLOYEE_H
#include "Employee.h"
using namespace std;
class partTimeEmployee : public Employee{
public:
partTimeEmployee();
partTimeEmployee(string x);
~partTimeEmployee();
string hourly_wage;
};
#endif
partTimeEmployee.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib.h>
#include "partTimeEmployee.h"
using namespace std;
partTimeEmployee::partTimeEmployee(){
cout<<"in default partTimeEmployee constructor"<<endl;
}
partTimeEmployee::~partTimeEmployee(){
cout<< " calling FTP destructor. " << endl;
}
partTimeEmployee::partTimeEmployee(string x){
hourly_wage = x;
cout << "partTimeEmployeeconstructor" <<endl;
}
test.cpp
#include <iostream>
#include <iomanip>
#include "sfasu.h"
#include "Employee.h"
#include "partTimeEmployee.h"
using namespace std;
int main(){
partTimeEmployee one("data");
}
Change
class partTimeEmployee : public partTimeEmployee
to
class partTimeEmployee : public Employee
When you build application you first compile source files (usually .cpp) to object files (usually .o or .obj) and then link them together into executable. It can be done explicitly through command line, using makefile or such or IDE. Your error shows that object file from compilation of partTimeEmployee.cpp is not included in linking. You do not provide enough information how you build your executable so it is difficult to say how to fix it.