BartQ
BartQ

Reputation: 65

already defined class C++

I don't understand one think. I have a header file called "network.h", and there are declaration of the class with functions. In "network.cpp" file I've these functions. When want include "network.h" in main.cpp and compile my project (Microsoft Visual Studio 2007) I got this message:

network.obj : error LNK2005: "class networkClass network" (?network@@3VnetworkClass@@A) already defined in main.obj

I know that we can't for example create two the same variable, but in this I don't know what's wrong.

source:

network.h

#ifndef H_NETWORK
#define H_NETWORK

    #include <string>
    #include <SFML/Network.hpp>




    struct getClientsStruct
    {
        std::string nick;
        sf::IpAddress clientIp;

    };

    getClientsStruct receiveClientIfno();

    class networkClass
    {
    public:
        void sendMyInfo();
        void bind();
        getClientsStruct receiveClientIfno();


    };

    networkClass network;


    #endif 

network.cpp

#include <SFML/Network.hpp>
#include "network.h"
#include <iostream>
#include <string>


sf::UdpSocket searchSocket;

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!





    void sendMyInfo()
    {
        sf::UdpSocket searchSocket;


        std::string myLocalAddress = sf::IpAddress::getLocalAddress().toString();

        char dataSend[100] = "DANE.......";
        sf::IpAddress recipient = "255.255.255.255";
        unsigned short portSending = 54000;
        if (searchSocket.send(dataSend, 100, recipient, portSending) != sf::Socket::Done)
        {
            // error...
            std::cout << "Sending ERROR" << std::endl;
        }
        else
            std::cout << "Sending SUCCESSED" << std::endl;
    }

    void bind()
    {
        // bind the socket to a port
        if (searchSocket.bind(54000) != sf::Socket::Done)
        {
            std::cout << "ERROR binding" << std::endl;
        }
        else
            std::cout << "BIND success" << std::endl;

    }

    getClientsStruct receiveClientIfno()
    {
        getClientsStruct ClientInfo;
        searchSocket.setBlocking(0);
        char dataReceived[100];
        std::size_t received;
        sf::IpAddress sender;
        unsigned short portReceive;






        if (searchSocket.receive(dataReceived, 100, received, sender, portReceive) != sf::Socket::Done)
            {   // error...
                std::cout << "Receive ERROR" << std::endl;
            }



        std::cout << "Received " << received << " bytes from " << sender << " on port " << portReceive << std::endl;


        return ClientInfo;

    }

----------------------------------EDIT-------------------------------------

So, I removed networkClass network; from network.h and declared it in main.cpp. Now when I want run function for example network.bind(); I got error: main.obj : error LNK2019: unresolved external symbol "public: void __thiscall networkClass::bind(void)" (?bind@networkClass@@QAEXXZ) referenced in function _main

Upvotes: 0

Views: 2090

Answers (2)

lrleon
lrleon

Reputation: 2648

You have most likely done two separate compilations. A compilation for network.cpp and probably another compilation for where is the main() function.

In `network.h", you declare the variable

networkClass network;

So if you include in the source where it is the main() function the header network.h, then the compiler will read the variable network as a instance.

But then, during the linking, it is detected that the variable network is duplicated, both in network.o and main.o

What you should do is specify in network.h

extern networkClass network;

Then in network.cpp you instantiate it (as a global variable if that is your intention).

Upvotes: 1

Christophe
Christophe

Reputation: 73510

Headers are meant to be included in several compilation units (cpp files). Unfortunately, in network.h you've defined a global object network. It will hence be declared several times (once when you compile network.cpp and another time when you compile main.cpp).

Global variables (if they can't be avoided by other means) should appear in headers only as extern:

    extern networkClass network;  // you say this variable exist somewhere else

You have then to put the definition in one of your cpp file. By the way, if it is not absolutely needed for your class, you shouldn't define it in the class header. Define it rather in main.cpp. And if you could avoid the global variable at all, get rid of it.

The other problem you have in network.cpp, is that your syntax defines global class independent, global, functions. Whenever you define a class function outside the class, you must prefix its name with the class name. For example:

void networkClass::bind()  // say which class the function is a member
...

Upvotes: 2

Related Questions