Ok, I have a header file that each of my source files includes:

#ifndef MAIN_H
#define MAIN_H
#include <string>
#include <vector>
#include <sys/socket.h>

using namespace std;

extern string CONFIG_FILE;

struct _msg {
string prefix;
string command;
vector<string> params;
};

struct _channel {
bool bLocalOnly;
string name;
vector<string> users;
string mode;
string topic;
vector<string> banlist;
};

struct _user { //List of all clients on the network
string username;
string host;
string nick;
string mode;
};

struct _server { //List of all servers in the network
string name;
int hopcount;
};

struct _route { //For message routing to clients and servers
bool bIsClient;
string dest;
string nexthop;
int metric;
};

//Lines

struct _oline { //Operator Lines
string host;
string username;
string password;
bool bIsGlobal;
};

struct _kline { //Kill Lines
string usermask;
string reason;
};

struct _dline { //IP Block Lines
string ipmask;
string reason;
};

struct _eline { //Exempt Lines
string hostmask;
string username;
string reason;
};

struct _iline { //Allowed Connection Lines
string ipmask;
string password;
string domainmask;
};

struct _cline { //Outgoing Server Lines
string hostname;
string password;
string servername;
int port;
};

struct _nline { //Incoming Server Lines
string hostname;
string password;
string servername;
};

struct _pline { //Server Binding Lines
string hostmask;
int port;
};

struct _qline { //Server Disallow Lines
string server;
string reason;
};

struct _uline { //Privileged Server Lines
string server;
string password;
string reason;
};

struct mline { //Server Line
string hostname;
string description;
};

struct aline { //Admin Line
string provider;
string serverdesc;
string admin;
};

vector<_msg> messages; //<-------Compiler hates these.
vector<_channel> channels;
vector<_user> users;
vector<_server> servers;
vector<_route> routes;
vector<_oline> olines;
vector<_kline> klines;
vector<_dline> dlines;
vector<_eline> elines;
vector<_iline> ilines;
vector<_cline> clines;
vector<_nline> nlines;
vector<_pline> plines;
vector<_qline> qlines;
vector<_uline> ulines;
#endif

I tried to extern them and put them in one of my files, main.cpp. I still get errors. Here is a small sample of the compiler error:

/tmp/ccrPupuq.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.3/ext/new_allocator.h:76: multiple definition of `messages'
/tmp/ccxIyi8l.o:/home/brian/work/dev/sdirc/args.cpp:29: first defined here
/tmp/ccrPupuq.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.3/bits/stl_vector.h:150: multiple definition of `channels'
/tmp/ccxIyi8l.o:/home/brian/work/dev/sdirc/args.cpp:29: first defined here
/tmp/ccrPupuq.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.3/bits/stl_construct.h:129: multiple definition of `users'
/tmp/ccxIyi8l.o:/usr/include/c++/4.3/bits/stl_vector.h:109: first defined here
/tmp/ccrPupuq.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.3/bits/stl_construct.h:104: multiple definition of `servers'

When I remove the vector statements, everything works fine. The point of the vectors is to replace my linked list code that is buggy and unreliable.

Recommended Answers

All 3 Replies

multiple definition of `messages'

Should be a clue for you. It means that you have the same definition declare for 'messages'. It also tells you exactly where it is first declared and at what line. The rest of the errors are similar type.

It would be. However, no matter what I name them, it will still complain about duplicate 'whatever I named the vector'.

The problem here is that you are declaring an instance of a variable inside of your header file. You are essentially making messages or channels global variables. This would be fine if the header was only included by a single source file. Here's what's happening. Every source file that includes your header file will get its own seperate variable in the global scope with the given name. This works fine for compiling, as each object file doesn't care what is defined in other object files. However, when you link the objects together, the linker sees multiple variables with the same name in the same scope. This isn't legal, so it fails.

As a rule, it is bad practice to use global variables, so you're going down that wrong road. You should consider using a class with accessor functions for getting to the data. In any case, those variables should be declared within the scope of a source file.

I also notice that you used "using namespace std;" in your header file. This is also bad practice. Prefix all of your stl types with std:: in a header file, and only use "using namespace std;" in a source file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.