Developer forums (C::B DEVELOPMENT STRICTLY!) > Development
qu :- multiple definition of menu.
(1/1)
Rahul:
hi to all, I've centos 7 as client and centos 6.10 as mysql server in VM. I am trying to build a small project , sample project i got from internet, that is running in one single file
(" main.cpp ") but when i try to make it in multiple files it stuck in following errors:-
--- Code: ---
-------------- Build: Release in airlines (compiler: GNU GCC Compiler)---------------
g++ -Wall -fexceptions -O2 -std=c++11 -m64 -I/usr/include/mysql -Iinclude -c /opt/projects/cb/airlines/main.cpp -o obj/Release/main.o
g++ -Wall -fexceptions -O2 -std=c++11 -m64 -I/usr/include/mysql -Iinclude -c /opt/projects/cb/airlines/src/dbConn.cpp -o obj/Release/src/dbConn.o
g++ -L/usr/lib64/mysql -o bin/Release/airlines obj/Release/draw.o obj/Release/getchoice.o obj/Release/main.o obj/Release/src/airline.o obj/Release/src/dbConn.o -s -m64 -lmysqlclient -lmysqlclient_r
In file included from include/dbConn.h:14:0,
from /opt/projects/cb/airlines/main.cpp:1:
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
NULL};
^
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
In file included from include/dbConn.h:14:0,
from /opt/projects/cb/airlines/src/dbConn.cpp:1:
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
NULL};
^
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
include/../getchoice.h:16:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
obj/Release/main.o:(.data+0x0): multiple definition of `menu'
obj/Release/getchoice.o:(.data+0x0): first defined here
obj/Release/src/dbConn.o:(.bss+0x10): multiple definition of `conn'
obj/Release/main.o:(.bss+0x10): first defined here
obj/Release/src/dbConn.o:(.bss+0x0): multiple definition of `res'
obj/Release/main.o:(.bss+0x0): first defined here
obj/Release/src/dbConn.o:(.bss+0x8): multiple definition of `row'
obj/Release/main.o:(.bss+0x8): first defined here
obj/Release/src/dbConn.o:(.bss+0x18): multiple definition of `qstate'
obj/Release/main.o:(.bss+0x18): first defined here
obj/Release/src/dbConn.o:(.data+0x0): multiple definition of `menu'
obj/Release/getchoice.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 12 warning(s) (0 minute(s), 0 second(s))
--- End code ---
i am sure to have single definition of menu and other members.
sodev:
I am sure you have not ;D. Apparently your project uses multiple files so you have to include some headers in your main file. So either your headers are wrong and contain definitions instead of declarations or you even include the sources instead of the headers.
Rahul:
here is mine dbconn.h
--- Code: ---#ifndef DBCONN_H
#define DBCONN_H
#include<iostream>
#include<cstdio>
#include<fstream>
#include<sstream>
#include<string>
#include<cstdlib>
#include<mysql.h>
#include "../draw.h"
#include "../getchoice.h"
using namespace std;
// Global Variable
int qstate;
MYSQL* conn;
MYSQL_ROW row;
MYSQL_RES* res;
// Global Variable End
using std::cout;
class dbConn
{
public:
dbConn();
virtual ~dbConn();
static void ConnectionFunction();
protected:
private:
};
#endif // DBCONN_H
--- End code ---
here is dbconn.cpp
--- Code: ---#include "dbConn.h"
dbConn::dbConn()
{
//ctor
}
dbConn::~dbConn()
{
//dtor
}
void dbConn::ConnectionFunction()
{
conn = mysql_init(0);
if (conn)
{
cout << "Database Connected" << endl;
cout << "Press any key to continue..." << endl;
clrscr();
}
else
cout << "Failed To Connect! " << mysql_errno(conn) << endl;
conn = mysql_real_connect(conn, "serverora11gr2.db.net", "rahul", "rahul", "airlinedb", 0, NULL, 0);
if (conn)
{
cout << "Database Connected To MySql" << conn << endl;
cout << "Press any key to continue..." << endl;
}
else
cout << "Failed To Connect!" << mysql_errno(conn) << endl;
}
--- End code ---
here is dbconn.cpp
--- Code: ---#ifndef DBCONN_H
#define DBCONN_H
#include<iostream>
#include<cstdio>
#include<fstream>
#include<sstream>
#include<string>
#include<cstdlib>
#include<mysql.h>
#include "../draw.h"
#include "../getchoice.h"
using namespace std;
// Global Variable
int qstate;
MYSQL* conn;
MYSQL_ROW row;
MYSQL_RES* res;
// Global Variable End
using std::cout;
class dbConn
{
public:
dbConn();
virtual ~dbConn();
static void ConnectionFunction();
protected:
private:
};
#endif // DBCONN_H
--- End code ---
here is mine getchoice.h file :-
--- Code: ---#ifndef GETCHOICE_H_INCLUDED
#define GETCHOICE_H_INCLUDED
#include "draw.h"
#include <cstdlib>
#include <cstring>
#include <iostream>
char * menu[] ={
"1. Reserve Seat.",
"2. User Ticket.",
"3. Flights Schedule.",
"4. Display Passenger.",
"5. Flight Details.",
"6. Exit Program.",
NULL};
int getchoice(char *choices[], FILE *in, FILE *out);
#endif // GETCHOICE_H_INCLUDED
--- End code ---
here is mine main.cpp
--- Code: ---#include "dbConn.h"
#include "getchoice.h"
#include "draw.h"
int main(int argc, char**argv)
{
dbConn::ConnectionFunction();
cout << "Welcome To Airlines Reservation System"<< endl;
cout << "Airlines Reservation System Menu" << endl;
}
--- End code ---
where is second definition. please help.
BlueHazzard:
1) This is not a programming forum, so this type of question is out of scope...
2) If you are using c++, use c++ features, capsule your variables in structs, and pass them around. Global variables are bad in every sense and only lead to messy code and strange behavior...
3) Every time you use a header in a c file the content gets copied to the c file and this combination gets translated to a object file. So now the linker kicks in and combines multiple c files with multiple definitions of the same variable name to a binary file and gets confused, because there are multiple definitions of variables with the same name...
see https://stackoverflow.com/questions/8108634/global-variables-in-header-file
simple solution, put a extern infront of your variables in the header...
sodev:
--- Quote from: BlueHazzard on April 24, 2020, 02:06:52 pm ---simple solution, put a extern infront of your variables in the header...
--- End quote ---
And then get undefined references linker errors :P. Turning these definitions into declarations is only half the work, adding their definitions to the implementation the other half. A faster, better and more correct approach would be to move these variables into the class as members.
But anyway, these are very basic c/c++ errors because of lack of language knowledge and way off topic for this forum, better teach yourself c++ programming and head to a programming beginners forum.
Navigation
[0] Message Index
Go to full version