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

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.

Related

Referee.cpp:10:18: error: no matching function for call to ‘Human::Human()’ Referee::Referee()

I'm new to c++ and was having problem with doing some inheritance, does any one know why im getting this error? (its the only error I'm getting when compiling).
I compiled with g++ -o. Also I'm so sorry in advance if I'm doing a lot of things wrong here, I'm very new to c++. >_<
Please let me know how I can make my code better or more efficient.
computer.h
#ifndef RPS_H
#define RPS_H
#include <iostream>
#include <string>
#include <stdio.h>
class Computer
{
public:
Computer(std::string);
~Computer();
char charc;
};
#endif
human.h
#ifndef HUMAN_H
#define HUMAN_H
#include <iostream>
#include <string>
#include <stdio.h>
class Human
{
public:
Human(std::string);
~Human();
char charh;
};
#endif
referee.h
#ifndef REFEREE_H
#define REFEREE_H
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
class Referee : public Human{
public:
Referee();
~Referee();
bool Winneris();
};
#endif
Computer.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "computer.h"
using namespace std;
Computer::Computer(string char_c)
{
}
Computer::~Computer()
{
}
Human.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
using namespace std;
Human::Human(string char_h){
char_h=charh;
cout<<"r/p/s?"<<endl;
cin>>charh;
}
Human::~Human()
{
}
Referee.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "referee.h"
using namespace std;
Referee::Referee(){
}
bool Referee::Winneris(){
if (charh=='r'){
cout<<"draw"<<endl;
}
else if(charh=='p'){
cout<<"Victory!"<<endl;
}
else if(charh=='s')
{
cout<<"Defeat"<<endl;
}
return 0;
}
Referee::~ReReferee(){
}
main.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
#include "computer.h"
#include "referee.h"
using namespace std;
string char_h;
string char_c;
// main program
int main()
{
Human *round1h;
round1h = new Human(char_h);
Computer *round1c;
round1c = new Computer(char_c);
Referee *round1r;
round1r = new Referee();
round1r -> Winneris();
}
When you have written the parameterized constructor in respective classes. You have created the class objects, which call the default constructor which takes no parameter.
You have to define the default constructor as well in your respective classes.
Human::Human()
{}
Computer::Computer()
{}
Referee::Referee()
{}
Constructor types

Simple modification of Hello World

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.

Class definition and utilization errors

I am getting errors with the following code. The errors are incomplete type is not allowed and use of undefined type 'mGame'.
header.h:
//--Libraries
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//--Classes
class mGame;
Game.cc:
#include "header.h"
class mGame
{
private:
public:
bool intro();
};
Intro.cc:
#include "header.h"
bool mGame::intro() //--Line 3
{
printf("|-----------------------------|\n");
printf("\n Welcome to the Guessing Game!\n");
printf("\n|-----------------------------|\n");
return false;
}
The errors are both on line 3 of intro.cc. I tried finding a solution, but I couldn't for what I am doing.
header.h doesn't know any definitions of game.cc, you tell header.h only, that there is a class mGame. rename game.cc to game.h and include it into header.h and delete the line "class mGame;"
To be able to use mGame from Intro.cc, you have to move the class declaration into header.h (or into some other header file that you include from Intro.cc).
Having a forward declaration in header.h is not enough (that's what is meant by "incomplete type is not allowed").

Multiple definition of C++. How to split C++ program properly?

I wrote a program that works without problems, but the thing I am afraid of is that I get a lot of warnings, when I compile it with -Wall option ( it's program written in C and C++). Actually there is only one type of warning, but occurs many times : Multiple definition of .... ( contructors , destructors, and functions ). I thought I did it correcly, but obviously I am wrong. I have 9 files:
Server.h
Server.cpp - implements methods declared in Server.h
RankingCreator.h
RankingCreator.cpp - implements methods declared in RankingCreator.h
Parser.h
Parser.cpp - implements methods declared in Parser.h
PageHandler.h
PageHandler.cpp - implements methods declared in PageHandler.h
and
Main.cpp
- all header files are included in this file, because I use and combine
functionality of all classes here
Each .cpp file except Main.cpp contains only one corresponding .h file included, for instance Server.cpp contains #include "server.h" and no more .h/.cpp files listed above ( but it does contain headers like stdio.h and string.h ). I can post whole warning message here and code of classes, but the lenght of error is about 50 lines, and all classes would be about 1000 lines, so tell me if it is really needed to solve this. Any idea how to solve this? Do I have to make every function inline or something? Every header file has #if def block at the beginning.
EDIT:
Here is the warning log :
g++ LearnTidyCurl.cpp MyParser.cpp PageHandler.cpp RankingCreator.cpp Server.cpp -lcurl -ltidy -o -Wall Wynik
Here is code of one of my header files, see the way of ifdefs :
#ifndef RANKINGCREATOR_H_
#define RANKINGCREATOR_H_
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
//using namespace std;
struct rankingElement {
std::string url;
int rank;
bool operator() (rankingElement i, rankingElement j) { return (i.rank > j.rank);}
} ;
bool operator==(const rankingElement& elem, const std::string& url);
class RankingCreator {
public:
rankingElement compareRankingElements;
const static int MAX_QUERY_RESULT_SIZE = 20;
RankingCreator();
virtual ~RankingCreator();
bool checkPageRank( rankingElement rElement, std::vector<rankingElement> &ranking );
void insertIntoRanking( rankingElement rElement, std::vector<rankingElement>& ranking);
};
#endif /* RANKINGCREATOR_H_ */
I threw warning message out, because it makes this topic unreadable.
Btw. I use include guards auto-generated by Eclipse - shouldn't they be just fine? ( When creating a new class they are automatically created )
EDIT:
You can download gedit file with error log here :
http://www4.zippyshare.com/v/62324366/file.html
I didn't want to post 105-lined error here and in addition it is in crap format, so would not good look here.
Server.h :
#ifndef SERVER_H_
#define SERVER_H_
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <vector>
...body...
#endif /* SERVER_H_ */
PageHandler.h
#ifndef PAGEHANDLER_H_
#define PAGEHANDLER_H_
#include <tidy.h>
#include <buffio.h>
#include <stdio.h>
#include <errno.h>
#include <iostream>
#include <string.h>
#include <curl/curl.h>
#include <vector>
#include <algorithm>
#include <stdexcept>
... body ...
#endif /* PAGEHANDLER_H_ */
MyParser.h
#ifndef MYPARSER_H_
#define MYPARSER_H_
#include <vector>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <algorithm>
#include <queue>
#include <stdlib.h>
...body...
#endif /* MYPARSER_H_ */
Main.cpp
#include <stdio.h>
#include <iostream>
#include <queue>
#include "MyParser.h"
#include "PageHandler.h"
#include "RankingCreator.h"
#include "Server.h"
#define NO_ERROR 0
std::string convertIntToString(int input) {
std::ostringstream ss;
ss << input;
std::string tmpStr = ss.str();
return tmpStr;
}
int main(int argc, char **argv) {
... body ...
return 0;
}
MyParser.cpp
#include "MyParser.h"
PageHandler.cpp
#include "PageHandler.h"
Server.cpp
#include "Server.h"
RankingCreator.cpp
#include "RankingCreator.h"
Change your inclusion guards:
#ifndef FILENAME_H
#define FILENAME_H
//Code
#endif
And I bet your problem goes away. Obviously make sure each FILENAME_H is unique :) And remember - each header needs this around all it's code, but it shouldn't be in your source files.
Write your header files the following way:
#ifndef SERVER_H
#define SERVER_H
//...
#endif
They are called #include guards to avoid double inclusion problems. Read about them here
If you are ever developing on MVSC++, you can use #pragma once as a first line of each header, but the first solution is portable on every platform.

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>