How to use header functions in source file in C++? - c++

I'm new to C++ programming language and it is different from Java. I tried to use functions from a header I made but when I use a function from the header , Eclipse C++ IDE says that member declaration is not found except for the constructor while it is found in the header as public.
Car.h file (header) :
#include <string>
using namespace std;
class Car {
private :
string name;
string model;
int year;
int width;
int height;
int depth;
public :
Car ();
Car (string n, string m, int y, int w, int h, int d);
void setName(string n);
void setModel (string m);
void setYear (int y);
void setSize (int w, int h, int d);
string getName ();
string getModel();
int getYear();
int getWidth();
int getHeight();
int getDepth();
};
Car.cpp file (source)
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
Car::Car(string n, string m, int y, int w, int h, int d) { //works properly
name = n;
model = m;
year = y;
width = w;
height = h;
depth = d;
}
Car::getName() { // IDE says member declaration not found
return name;
}
Car::getModel() { // IDE says member declaration not found
return model;
}
Car::getYear() { // IDE says member declaration not found
return year;
}
Car::getWidth() { // IDE says member declaration not found
return width;
}
Car::getHeight () { // IDE says member declaration not found
return height;
}
What I have did wrong ?

All of your functions are missing the return type, for example
string Car::getName() {
return name;
}

The reason why Car works is because it is a Constructor and does not need a type declaration.
All the rest of your functions do.
int Car::getYear() { // IDE says member declaration not found
return year;
}

Do this :-
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
Car::Car(string n, string m, int y, int w, int h, int d) { //works properly
name = n;
model = m;
year = y;
width = w;
height = h;
depth = d;
}
string Car::getName() { // IDE says member declaration not found
return name;
}
string Car::getModel() { // IDE says member declaration not found
return model;
}
int Car::getYear() { // IDE says member declaration not found
return year;
}
int Car::getWidth() { // IDE says member declaration not found
return width;
}
int Car::getHeight () { // IDE says member declaration not found
return height;
}

Related

make prototype of overloading function c++

I want to make a overloading function with a prototype in C++.
#include <iostream>
using namespace std;
int rectangle(int p, int l);
int main() {
cout << rectangle(3);
return 0;
}
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
I got error at
int rectangle(int p, int l);
is that possible make prototype with a overloading function? if possible how to do it
You've to declare the function before you use/call it. You did declare the 2 argument version of rectangle function but you seem to forget to declare the 1 argument taking version.
As shown below if you add the declaration for the 1 argument version then your program works(compiles).
#include <iostream>
using namespace std;
//declare the function before main
int rectangle(int p, int l);
int rectangle(int p);//ADDED THIS DECLARATION
int main() {
cout << rectangle(3);
return 0;
}
//define the functions after main
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
The output of the program can be seen here.
Alternative solution:
If you don't want to declare each function separately then you should just define them before main instead of declaring them as shown below.
#include <iostream>
using namespace std;
//define the functions before main. This way there is no need to write a separate function declaration because all definition are declarations
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
int main() {
cout << rectangle(3);
return 0;
}

error: use of undeclared identifier 'std' c++

I am new to coding in C++ and I want to make a simple Pokemon game.
I created a class in a header file and I am defining the functions in a separate .cpp file.
I also have a main file where I will run my actual code.
So I am defining a std::string function in my functions file, and it says std is an undeclared identifier.
Here are each of my files:
Function Definition:
#include "fns.hpp"
int Pokemon::getHP() {
return hp;
}
int Pokemon::getAttack() {
return attack;
}
int Pokemon::getDefense() {
return defense;
}
int Pokemon::getSpecialAttack() {
return specialAttack;
}
int Pokemon::getSpecialDefense() {
return specialDefense;
}
int Pokemon::getSpeed() {
return speed;
}
std::string Pokemon::getAttack1() {
return attack1;
}
std::string Pokemon::getAttack2() {
return attack2;
}
std::string Pokemon::getAttack3() {
return attack3;
}
std::string Pokemon::getAttack4() {
return attack4;
}
Pokemon::Pokemon(int qhp,int qdefense,int qattack,int qspecialAttack,int qspecialDefense,int qspeed,std::string qattack1,std::string qattack2,std::string qattack3,std::string qattack4)
: hp(qhp),attack(qattack),defense(qdefense),specialAttack(qspecialAttack),specialDefense(qspecialDefense),speed(qspeed),attack1(qattack),attack2(qattack2),attack3(qattack3),attack4(qattack4) {}
Function Declaration:
class Pokemon {
int hp,attack,defense,specialAttack,specialDefense,speed;
std::string attack1,attack2,attack3,attack4;
public:
int getHP();
int getAttack();
int getDefense();
int getSpecialAttack();
int getSpecialDefense();
int getSpeed();
int getAttack1();
int getAttack2();
int getAttack3();
int getAttack4();
Pokemon(int qhp,int qdefense,int qattack,int qspecialAttack,int qspecialDefense,int qspeed,std::string qattack1,std::string qattack2,std::string qattack3,std::string qattack4);
};
Whenever I say std::string, it says it is an undeclared identifier.
Can someone please help me?
It is because you have not used the library for it.
use the below at the top of your header file
#include<string>

C++ Object declaration needs another object as parameter

Object-oriented C++ here.
I'm supposed to code a Microwave object that "heats" a FrozenMeal object.
One method of the Microwave object, called void heatMeal(FrozenMeal), is supposed to take an instance of a FrozenMeal object as a parameter and increase its temperature.
FrozenMeal.h
#include <string>
class FrozenMeal {
public:
FrozenMeal(std::string, int);
void setTemperature(double);
std::string getName() const;
int getVolume() const;
double getCoeffizient() const;
double getTemperature() const;
private:
std::string name;
int volume;
double temperature;
double coeffizient;
};
FrozenMeal.cpp
#include <string>
#include "FrozenMeal.h"
using namespace std;
FrozenMeal::FrozenMeal(string mealName, int mealVolu) {
name = mealName;
volume = mealVolu;
temperature = -18;
coeffizient = 0.24;
}
void FrozenMeal::setTemperature(double mealTemp) { temperature = mealTemp; }
string FrozenMeal::getName() const { return name; }
int FrozenMeal::getVolume() const { return volume; }
double FrozenMeal::getCoeffizient() const { return coeffizient; }
double FrozenMeal::getTemperature() const { return temperature; }
Microwave.h
#include "FrozenMeal.h"
class Microwave {
public:
Microwave();
void morePower();
void lessPower();
void setPeriod(double);
void heatMeal(FrozenMeal); // <----------------------------
int getPower() const;
double getPeriod() const;
private:
int power;
double period;
};
Microwave.cpp
#include "Microwave.h"
using namespace std;
Microwave::Microwave() {}
void Microwave::morePower() { if (power < 1000) power += 200; }
void Microwave::lessPower() { if (power > 200) power -= 200; }
void Microwave::setPeriod(double sessionPeri) { period = sessionPeri; }
void Microwave::heatMeal(FrozenMeal mealInst) {
mealInst.setTemperature(80); //example
}
int Microwave::getPower() const { return power; }
double Microwave::getPeriod() const { return period; }
Now, my problem is that my compiler says that the file FrozenMeal.h apparently redefines the object type of FrozenMeal, even though that should be the job of the FrozenMeal.cpp file, and compiling is unsuccessful.
I tried including FrozenMeal.h to Microwave.cpp but that resulted in even more compiler errors.
I feel like I'm doing something horribly wrong here.
Add include guards to your header files so its contents doesn't get included more than once:
FrozenMeal.h:
#ifndef FROZENMEAL_H_INCLUDED
#define FROZENMEAL_H_INCLUDED
// your code ...
#endif /* FROZENMEAL_H_INCLUDED */
Microwave.h:
#ifndef MICROWAVE_H_INCLUDED
#define MICROWAVE_H_INCLUDED
// your code ...
#endif /* MICROWAVE_H_INCLUDED */
Also, you never initialize int Microwave::power and double Microwave::period so you will read and write garbage values in Microwave::morePower() and Microwave::lessPower()
As suggested in the comments, you want to take the parameter of Microwave::heatMeal() by reference so the function can modify the passed object:
void Microwave::heatMeal(FrozenMeal &mealInst)
// ^

2 classes with the same name within namespaces

I have gone back to learning C++ doing some old university courses and I am now currently learning parametric polymorphism as well as creating my own namespaces.
The exercise states that I have to make a namespace called "Federation" which has a class called "Ship" that takes values and one default value that never changes.
inside the federation namespace there is also a "Starfleet" namespace in which we also have a "Ship" class, the only difference is that the default value stated before can be specified by the user.
Here is the code:
Federation.hpp
#include <iostream>
#include <string>
#include <cstring>
namespace Federation
{
namespace Starfleet
{
class Ship
{
public:
Ship(int length, int width, std::string name, short maxWarp);
~Ship();
private:
int _length;
int _width;
std::string _name;
short _maxWarp;
};
};
class Ship
{
public:
Ship(int length, int width, std::string name);
~Ship();
private:
int _length;
int _width;
std::string _name;
}
};
Federation.cpp
#include "Federation.hpp"
using namespac std;
Federation::Starfleet::Ship::Ship(int length, int width, string name, short maxWarp): _length(length), _width(width), _name(name), _maxWarp(maxWarp)
{
cout << "Starfleet Ship Created." << endl;
}
Federation::Starfleet::Ship::~Ship()
{
}
Federation::Ship::Ship(int length, int width, string name, int speed = 1): _length(length), _width(width), _name(name)
{
cout << "Regular Ship Created"
}
Federation::Ship::~Ship()
{
}
main.cpp
#include "Federation.hpp"
int main(int ac, char **av)
{
Federation::Starfleet::Ship mainShip(10, 10, "Starfleet Ship", 20);
Federation::Ship smallShip(5, 5, "Small Ship");
}
When compiling I get this Error: "prototye for Federation::Ship::Ship(int, int, std::__cxx11::string, int) does not match any class in Federation::Ship"
I am totally lost as to what this means, when I look at my functions on my hpp file all of them seem to be correct, so I don't really understand what exactly I'm doing wrong in this case.
This has nothing to do with namespaces. You declare the c'tor with a certain prototype in the header:
Ship(int length, int width, std::string name);
And then randomly add a parameter with a default argument in the implementation file:
Federation::Ship::Ship(int length, int width, string name, int speed = 1)
Argument types are a part of any function or constructor's signature. So you have a declaration and definition mismatch. Declare the extra parameter in the header (along with the default argument).
Ship(int length, int width, string name, int speed = 1);
// and
Federation::Ship::Ship(int length, int width, string name, int speed)

Way around "first defined here" error?

I need to have two alternate classes with the same name, that I can switch between each other by simply changing which class is included in main.
For example;
Mode_1.h
class Draw{
private:
// private stuff
public:
void Render(int x, char y);
};
Mode_2.h
class Draw{
private:
// private stuff
public:
void Render(int x, char y);
};
main.cpp
#include "Mode_1.h"
int main(){
Draw D;
int x = 2;
char y = 'x';
D.Render(x, y);
}
Currently I'm having to comment out the .h and .cpp files I'm not using to avoid the "first defined here" error. What I want is that all I have to do to switch between them is change
#include "Mode_1.h"
to
#include "Mode_2.h"
You should put them in different namespaces:
namespace Mode2
{
class Draw{
private:
// private stuff
public:
Draw(int x, char y);
};
}
In main you can then select the namespace you want to use:
#include "Mode_1.h"
#include "Mode_2.h"
using namespace Mode2;
int main()
{
Draw D;
int x = 2;
char y = 'x';
D.Draw(x, y);
return 0;
}
You may try like this:
#ifdef MODE1
#include "Mode_1.h"
#else
#include "Mode_2.h"
#endif
int main(){
Draw D;
int x = 2;
char y = 'x';
Draw(x, y);
}
And compile this source file with -DMODE1 or none depending on you wish to include Mode_1.h or Mode_2.h