Simple modification of Hello World - c++

I'm a beginner C++ programmer. There has to be simple mistake somewhere but I can't figure it out why it won't compile.
Main.cpp:
#include <iostream>
#include <string>
#include "GUI.h"
#include "GUI.cpp"
int main()
{
Display("Hello World!");
return 0;
}
GUI.h
#pragma once
void Display(std::string param0);
GUI.cpp
#include "GUI.h"
void Display(std::string param0)
{
std::cout << param0;
}
Errors are:
Namespace "std" has no member "string" and "cout"
'String' is not a member of 'std'
'String' undeclared identifier
syntax error: missing ')' before identifier 'param0'
'{': missing function header (old-style formal list?)
However, when I paste code directly into Main.cpp creating
#include <iostream>
#include <string>
void Display(std::string param0);
void Display(std::string param0)
{
std::cout << param0;
}
int main()
{
Display("Hello World!");
return 0;
}
It works fine so the problem lies probably with incorrect use of #includes.
I always thought that the include directive just "injects" code into main.cpp and it's just for organizational purposes and for dividing code into smaller pieces but now I'm confused.
How this code should look like and why?
Are there already any bad programming habits in this piece of code?
EDIT: Thanks for help, it finally compiles so I assume it should be done that way:
Main.cpp
#include <iostream>
#include <string>
#include "GUI.h"
int main()
{
Display("Hello World!");
return 0;
}
GUI.h
#pragma once
#include <iostream>
#include <string>
void Display(std::string param0);
can also be done as
#ifndef GUI_H
#define GUI_H
#include <iostream>
#include <string>
void Display(std::string param0);
#endif
GUI.cpp
#include "GUI.h"
void Display(std::string param0)
{
std::cout << param0;
}

You have forgot to add #include <string> into GUI.h. There is no need to include GUI.cpp file.

Related

identifier "test" is undefined; What does this mean in this context

I am writing a weather station for a raspberry. I'm always getting the error message identifier "test" is undefined.
I've already tried to use no external class with a little example and this works perfectly. Now I'm trying to create an object test, but this doesn't work. I am always getting the error message:
E0020 identifier "test" is undefined
main.cpp:
#include <wiringPi.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <errno.h>
#include <stdlib.h>
include "MeasureWeather.h"
int main(void)
{
MeasureWeather test;
while (1)
{
test._setSensorPin(DHT_PIN);
}
return 0;
}
MeasureWeather.h:
#ifndef MeasureWeather
#define MeasureWeather
#include <wiringPi.h>
#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <stdlib.h>
#include <cstring>
class MeasureWeather
{
public:
//setter for sensor pin
void _setSensorPin(uint8_t pin);
private:
uint8_t sensorPin;
};
#endif // !MeasureWeather
MeasureWeather.cpp:
include "MeasureWeather.h"
void MeasureWeather::setSensorPin(uint8_t pin)
{
_sensorPin = pin;
}
I hope somebody can help me with my issue. Thank you!
You have this at the top of your header file, as part of the include guard:
#define MeasureWeather
MeasureWeather is the name of your class. By defining it as a macro, you hide the class name. Thus the line
MeasureWeather test;
expands to
test;
which would be a reference to something called test, not a declaration.
Use a different identifier for your #include guard.

Main file don't sees incuded header in stdafx.h

Hey I have strange problem...
When I'm adding a class into my project in VS 2015
Im including new class into stdafx.h, and stdafx I including to file with main function. When I compiling a program, VS throws Error:
I show it on the code:
stdafx.h
#pragma once
#include <time.h>
#include <map>
#include "targetver.h"
#include <math.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <vector>
#include <deque>
#include <GL/freeglut.h>
#include "FreeImage.h"
#include "vec3.h"
#include "vec2.h"
#include "vec4.h"
#include "TextureManager.h"
#include "SceneObject.h"
#include "Obj3d.h"
#include "Door.h"
#include "Collider.h"
#include "TrashGen.h"
#include "Player.h"
#include "Scene.h"
Door.h:
class Door
{
public:
Door();
~Door();
int key;
};
Door.cpp:
#include "stdafx.h"
#include "Door.h"
Door::Door()
{
key = 10;
}
Door::~Door()
{
}
When i add in main function:
in this file I included stdafx.h
Door *dor;
i have an error:
Error C2065 'dor': undeclared identifier IROBOTGAME
Error C2065 'Door': undeclared identifier IROBOTGAME
Sometimes, Visual C++ looses its way with the precompiled headers, depending on the sequence of your edits.
Simply do a full 'Rebuild', that might fix it.

Sharing a defined global variable in a header amongst two cpp files

I am interested in being able to share a defined global variable across two cpp files. Is the following possible? I am interested in this to avoid having to initialize the global shared variable multiple times. I am having trouble being able to build this code. How would you recommend to declare/define myMap in this case?
MapHeader.h
#ifndef _MAP_HEADER_
#define _MAP_HEADER_
#include <string>
#include <map>
using namespace std;
extern const map<int, string> myMap = {{100 , "one hundred"}, {200,"two hundred"}, {300,"three hundred"}};
#endif // _MAP_HEADER_
FirstFile.h
#ifndef _FIRST_FILE_
#define _FIRST_FILE_
#include "MapHeader.h"
#include <iostream>
#include <string>
using namespace std;
void myFunction1();
#endif
FirstFile.cpp
#include "FirstFile.h"
void myFunction1(){
cout << "myFunction1()" << myMap[100] << endl;
}
SecondFile.h
#ifndef _SECOND_FILE_
#define _SECOND_FILE_
#include "MapHeader.h"
#include <iostream>
#include <string>
using namespace std;
void myFunction2();
#endif
SecondFile.cpp
#include "SecondFile.h"
void myFunction2(){
cout << "myFunction2()" << myMap[200] << endl;
}
Main.cpp
#include "FirstFile.h"
#include "SecondFile.h"
int main()
{
myFunction1();
myFunction2();
return 0;
}
I am getting the error message:
error: passing 'const std::map<>' as 'this' argument of 'std:map<>' .....
In MapHeader.h, change your definition to extern map myMap; and then move your definition exactly as you had it in MapHeader.h into one of the .cpp's.

"Expected unqualified-id" for ofstream

I wrote the following C++ program, but at the line where I used out_stream.open(), it keeps telling me that there are errors about "unknown typename 'out_stream'" and "Expected unqualified-id".
I am new to C++ and I think I just copied down the lines from my textbook, so I can't figure out where it's wrong. Please bear with me if it is a really simple mistake.
Here's my code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <boost/random.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/math/distributions.hpp>
std::ofstream out_stream;
out_stream.open("output.txt");
int main()
{
std::cout<<"hello world!";
return 0;
}
You cannot to this
out_stream.open("output.txt");
outside of a function. Put it inside the main().
int main()
{
out_stream.open("output.txt");
std::cout<<"hello world!";
return 0;
}

What's wrong with this C++ program?

I just have an header file and and an .cpp file i am just passing an value to function but it gives me an error
main.c
#include "me.h"
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
int main()
{
me("http");
}
me.h
#ifndef ME_H_
#define ME_H_
#include <string.h>
class me {
public:
me(std::string u);
virtual ~me();
};
#endif /* ME_H_ */
me.cpp
#include "me.h"
#include <iostream>
#include <string.h>
using namespace std;
me::me(std::string u) {
// TODO Auto-generated constructor stub
cout << "help";
}
me::~me() {
// TODO Auto-generated destructor stub
}
I am getting an error
In file included from ../src/me.cpp:8:
../src/me.h:13: error: expected ‘)’ before ‘u’
../src/me.cpp:12: error: prototype for ‘me::me(std::string)’ does not match any in class ‘me’
../src/me.h:11: error: candidates are: me::me(const me&)
../src/me.h:11: error: me::me()
make: *** [src/me.o] Error 1
#include <string> instead of #include <string.h>
string.h is the C string header, accessible in C++ as <cstring>
<string> is the C++ header that defines std::string
you want #include <string> instead of #include <string.h>