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.
Related
I need to implement two classes.
One should contain an Employee with no methods, just constructor.
Second one should have std::vector container in with all the Employers made inside it, and few methods to manipulate those objects for example to add new employee or change his id.
After many attempts with errors im stuck and i dont know how to pass created employees to container in second class. I dont even know how the HR class should look like (its constructor should only have container?) and how it can make this container of Employee class objects.
First class is Employee.h, Employee.cpp and second is HR.h, HR.cpp
Here is my Employee.h
#include <iostream>
class Employee {
public:
std::string id;
std::string name;
std::string surname;
std::string departmentId;
std::string position;
Employee(std::string id, std::string name, std::string surname, std::string departmentId, std::string position);
~Employee();
};
Employee.cpp
#include "Employee.h"
#include <iostream>
#include <vector>
Employee::Employee(std::string xid, std::string xname, std::string xsurname, std::string xdepartmentId, std::string xposition) : id (std::move(xid)), name (std::move(xname)), surname (std::move(xsurname)), departmentId (std::move(xdepartmentId)), position (std::move(xposition))
{
std::cout << "constructor of worker"<< std::endl;
std::cout << "id:" + this->id + " name:" + this->name + " surname:" + this-> surname + " department:" + this->departmentId + " position:" + this->position << std::endl;
};
Employee::~Employee()
{
std::cout << "destructor"<< std::endl;
};
main.cpp
#include <iostream>
#include <vector>
#include "Employee.h"
#include "HR.h"
int main() {
Employee first("2","John","Smith","1","c++ developer");
Employee second("3","Steven","McDonald","2","administrator");
Employee third("4","Mark","Johnson","1","c++ developer");
system("pause");
return 0;
}
Should i place this push_back in Employee constructor and throw it somehow to HR class everytime new employee is created?
I'm really stuck right here.
EDIT: HR.h code - it's almost nothing there. std::vector is now created by method of HR. I dont know how HR constructor should look like and how can it implement objects from Employee class to vector container.
HR.h
#include "Employee.h"
#include <vector>
class HR {
public:
void addEmp(Employee);
HR();
~HR();
};
HR.cpp
#include <iostream>
#include <vector>
#include "HR.h"
HR::HR()
{
std::cout << "constructor HR"<< std::endl;
};
void HR::addEmp(Employee)
{
std::vector < Employee > all;
};
HR::~HR()
{
std::cout << "destructor HR"<< std::endl;
};
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.
I don't understand this error
here is a link to view the code online:
https://onlinegdb.com/rkirYvU_M
I am trying to add the names of the drivers, owners, and model to vectors, and we need to use pointers and files.
Here is my main file:
#include "person.h"
#include "car.h"
#include <iostream>
#include <vector>
std::vector <Person*>people;
std::vector <Car*> cars;
int main()
{
bool done = false;
Person person;
while(! done)
{
std::cout << "\n Please enter the owners ";
Person*prompt_info();
std::cout << "\n Please enter the drivers ";
Car*prompt_info();
Car*set();
Car*print();
}
return 0;
}
Here is the person.h file:
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <iostream>
//using namespace std;
class Person
{
public:
Person();
std::string get_name();
int get_age();
void prompt_info();
private:
std::string name;
int age;
};
#endif
Here is the person.c++ file:
#include "person.h"
Person::Person()
{
}
void Person::prompt_info()
{
std::cout << " name: ";
std::cin >> name;
std::cout << "enter their age: ";
std::cin >> age;
}
std::string Person::get_name()
{
return name;
}
int Person::get_age()
{
return age;
}
Here is the car.h file:
#ifndef CAR_H
#define CAR_H
#include <string>
#include <iostream>
#include "person.h"
using namespace std;
class Car
{
public:
Car();
std::string get_model();
Person* get_owner();
Person* get_driver();
void print();
void set(Person _owner,Person get_driver);
void prompt_info();
private:
std::string model;
Person* owner;
Person* driver;
};
#endif
I am trying to understand this error.
main.cpp:23:25: error: ambiguating new declaration of 'Car* prompt_info()'
Car*prompt_info();
^
You seem to be confusing function declarations with member functions. Just declare a Person object on the stack and call the method through it's object. Do the same for your Car object. You can use your objects like this.
while(! done)
{
Person person; ///< Person object named 'person'
Car car; ///< Car object named 'car'
std::cout << "\n Please enter the owners ";
person.prompt_info();
std::cout << "\n Please enter the drivers ";
car.prompt_info();
car.set();
car.print();
// TODO do something with your objects (store to vector?)
// next time through the loop your person and car will
// get initialized all over again
}
return 0;
You will have to store your temporary objects before they go out of scope if you want to use them later.
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
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.