Can't get this static class linked.

Discussion in 'C++' started by sam, May 13, 2005.

  1. sam

    sam Guest

    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
     
    sam, May 13, 2005
    #1
    1. Advertising

  2. sam

    Pete Becker Guest

    sam wrote:
    > 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.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, May 13, 2005
    #2
    1. Advertising

  3. * 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.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 13, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    514
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    503
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    548
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    705
    John Carson
    Oct 2, 2006
  5. Robin
    Replies:
    0
    Views:
    439
    Robin
    Jun 6, 2007
Loading...

Share This Page