Can't get this static class linked.

S

sam

Hi,

I've written the following static class definition :
#ifndef __AUTH__
#define __AUTH__

#include <iostream>
#include <string>
#include <fstream>

#include "fileutils.hh"

using namespace std;


class Auth
{
public:
Auth();
void reload_licence(ifstream &conf);
void save();
static bool validate() {return false;};

~Auth() {};

private:
static string _seed;
static string key;
static string period;
};

string Auth::_seed = "sakjfskjfsk342989sj_+)_()*";

#endif

but the linking is failed with the following error:
auth.o(.bss+0x0): multiple definition of `Auth::_seed'
auth.o(.bss+0x4): multiple definition of `Auth::key'
auth.o(.bss+0x8): multiple definition of `Auth::period'

How can I fix this error?

Thanks
Sam
 
P

Pete Becker

sam said:
Hi,

I've written the following static class definition :

There is no such thing as a "static class" in C++. What you've posted
here is a class definition.
#ifndef __AUTH__

Illegal. Don't use names that contain two adjacent underscores, or names
that begin with an underscore followed by a capital letter. But that's
not the problem.
string Auth::_seed = "sakjfskjfsk342989sj_+)_()*";

but the linking is failed with the following error:
auth.o(.bss+0x0): multiple definition of `Auth::_seed'
auth.o(.bss+0x4): multiple definition of `Auth::key'
auth.o(.bss+0x8): multiple definition of `Auth::period'

How can I fix this error?

Don't define data objects in headers. The header defines Auth::_seed, so
every translation unit that includes that header will have a copy of it.
That will produce this sort of error message. There is no code in your
message that defines Auth::key or Auth::period, so it's really not
possible to say what causes the last two error messages.
 
A

Alf P. Steinbach

* sam:
I've written the following static class definition :

C++ doesn't have static classes.


#ifndef __AUTH__

Names starting with two underscores are reserved for the implementation.

#define __AUTH__

#include <iostream>

Don't include <iostream> in a header file unless you need it.

Ditto for other headers.

#include <string>
#include <fstream>

#include "fileutils.hh"

using namespace std;

Do not ever put 'using namespace std;' in a header file: it forces this on
all client code.

class Auth
{
public:
Auth();
void reload_licence(ifstream &conf);
void save();
static bool validate() {return false;};

~Auth() {};

private:
static string _seed;
static string key;
static string period;
};

string Auth::_seed = "sakjfskjfsk342989sj_+)_()*";

Don't put namescape scope extern variable definitions in header files.

Move it to an separately compiled implementation file.

(There are tricks that allow you to avoid an implementation file, but for
now the main thing is to get the code working.)

#endif

but the linking is failed with the following error:
auth.o(.bss+0x0): multiple definition of `Auth::_seed'
auth.o(.bss+0x4): multiple definition of `Auth::key'
auth.o(.bss+0x8): multiple definition of `Auth::period'

How can I fix this error?

See above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top