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

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.

Related

std::any bad cast whenever a function inside a header is implemented in a cpp file returns it

This is my main.cpp
#include <iostream>
#include <unordered_map>
#include <string>
#include "myclass.h"
int main(int argc, const char * argv[]) {
any a = myclass::returnthis();
unordered_map<string, any>* hmap = any_cast<unordered_map<string, any>*>(a);
return 0;
}
this is myclass.h
#ifndef myclass_h
#define myclass_h
#include <any>
using namespace std;
class myclass {
public:
static any returnthis();
};
#endif /* myclass_h */
and this is myclass.cpp
#include "myclass.h"
#include <any>
#include <unordered_map>
#include <string>
any myclass::returnthis() {
return new unordered_map<string, any>();
}
Now, any_cast will report bad any cast. I am working on macOS and I have tried Xcode and CLion.
However, if I put the implementation of the function inside the header file, the problem vanishes. Why?

C++ use of undeclared identifier IOKit Functions

I am getting an error building my c++ program on xcode. For each of the various fuctions of the IOKit I am getting "use of undeclared identifier". I really don't know why I am getting this error. Here is the code:
#include <iostream>
#include <xlnt/xlnt.hpp>
#include <xlnt/xlnt_config.hpp>
#include <IOKitLib.h>
#include <IOTypes.h>
#include <IOReturn.h>
#include <IOKitKeys.h>
#include <iokitmig.h>
#include <OSMessageNotification.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <paths.h>
#include <termios.h>
#include <sysexits.h>
#include <sys/param.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/IOBSD.h>
#define LOCAL_ECHO
#ifdef LOCAL_ECHO
#define kOKResponseString “AT\r\r\nOK\r\n”
#else
#define kOKResponseString “\r\nOK\r\n”
#endif
#define kATCommandString "AT\r"
#define kMyErrReturn -1
enum {
kNumRetries = 3
};
static struct termios gOriginalTTYAttrs;
int main()
{
char path;
char text;
int fileDescriptor;
kern_return_t kernResult;
io_iterator_t serialPortIterator;
char deviceFilePath[MAXPATHLEN];
kernResult = MyFindModems(&serialPortIterator);
kernResult = MyGetModemPath(serialPortIterator, deviceFilePath,
sizeof(deviceFilePath));
Here is the image with all the errors shown: https://i.stack.imgur.com/e1UZS.jpg
Any advice would be more than appreciated. Is it because I haven't declared the functions?
Thanks in advance
Alex

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.

Nested #include and error : fatal error: map : No such file or directory

I wrote a multiple files program in C/C++ and create library.h and put all of *.h in library.h, Then #include "library.h" same the following code :
#ifndef LIBRARY_H_
#include "library.h"
#endif
My library.h is here:
#ifndef LIBRARY_H_
#define LIBRARY_H_
#include <map>
#include <fstream>
#include <string>
#include <string.h>
#include <cstdio>
#include <iostream>
#include <ncurses.h>
#include <stdlib.h>
//#include <termios.h>
using namespace std;
#include "structs.h"
#include "globals.h"
#include "typedef.h"
#include "readfile.h"
#include "writefile.h"
#include "tui.h"
#endif /* LIBRARY_H_ */
Please note that I get the following error:
fatal error: map: No such file or directory
and map is first #include file. I commented it, then map changed to fstream (next of map).
When I remove #include "library.h" from tui.h everything is ok! tui.h is here:
#ifndef TUI_H_
#define TUI_H_
#ifndef LIBRARY_H_
#include "library.h"
#endif
//#endif LIBRARY_H_
//BEGIN_C_DECLS
void print_menu(WINDOW *menu_win, int highlight);
void menu();
//END_C_DECLS
#endif /* TUI_H_ */
How can I solve it?