2016-05-29 4 views
-1

私は私の再定義の問題のいくつかを解決しますが、まだ1を持っている:再定義

エラー2エラーLNK2005: "クラスConsoleCommandHandler commandHandler"(commandHandler @@ 3VConsoleCommandHandler @@ A?)すでにIRC.obj Cで定義されています:\ Users \ユーザールカシュ\デスクトップ\ IRCClientマスターMagic.obj \ここ

は.hファイルである

magic.h

#ifndef Magic_h 
#define Magic_h 
#include <iostream> 
#include <signal.h> 
#include <cstdlib> 
#include <map> 
#include <algorithm> 
#include "src\Thread.h" 
#include "src\IRCClient.h" 



void signalHandler(int signal); 

class ConsoleCommandHandler 
{ 
public: 
    bool AddCommand(std::string name, int argCount, void(*handler)(std::string /*params*/, IRCClient* /*client*/)); 
    void ParseCommand(std::string command, IRCClient* client); 
private: 
    struct CommandEntry 
    { 
     int argCount; 
     void(*handler)(std::string /*arguments*/, IRCClient* /*client*/); 
    }; 

    std::map<std::string, CommandEntry> _commands; 
}; 

ConsoleCommandHandler commandHandler; 

void msgCommand(std::string arguments, IRCClient* client); 
void joinCommand(std::string channel, IRCClient* client); 
void partCommand(std::string channel, IRCClient* client); 


void ctcpCommand(std::string arguments, IRCClient* client); 

ThreadReturn inputThread(void* client); 

#endif 

magic.cpp

Magic.hファイルで
#include "Magic.h" 



void signalHandler(int signal) 
{ 
    volatile bool running; 
    running = false; 
}; 


bool ConsoleCommandHandler::AddCommand(std::string name, int argCount, void(*handler)(std::string /*params*/, IRCClient* /*client*/)) 
    { 
     CommandEntry entry; 
     entry.argCount = argCount; 
     entry.handler = handler; 
     std::transform(name.begin(), name.end(), name.begin(), towlower); 
     _commands.insert(std::pair<std::string, CommandEntry>(name, entry)); 
     return true; 
    } 

void ConsoleCommandHandler::ParseCommand(std::string command, IRCClient* client) 
    { 
     if (_commands.empty()) 
     { 
      std::cout << "No commands available." << std::endl; 
      return; 
     } 

     if (command[0] == '/') 
      command = command.substr(1); // Remove the slash 

     std::string name = command.substr(0, command.find(" ")); 
     std::string args = command.substr(command.find(" ") + 1); 
     int argCount = std::count(args.begin(), args.end(), ' '); 

     std::transform(name.begin(), name.end(), name.begin(), towlower); 

     std::map<std::string, CommandEntry>::const_iterator itr = _commands.find(name); 
     if (itr == _commands.end()) 
     { 
      std::cout << "Command not found." << std::endl; 
      return; 
     } 

     if (++argCount < itr->second.argCount) 
     { 
      std::cout << "Insuficient arguments." << std::endl; 
      return; 
     } 

     (*(itr->second.handler))(args, client); 
    } 

    struct CommandEntry 
    { 
     int argCount; 
     void(*handler)(std::string /*arguments*/, IRCClient* /*client*/); 
    }; 

    std::map<std::string, CommandEntry> _commands; 


void msgCommand(std::string arguments, IRCClient* client) 
{ 
    std::string to = arguments.substr(0, arguments.find(" ")); 
    std::string text = arguments.substr(arguments.find(" ") + 1); 

    std::cout << "To " + to + ": " + text << std::endl; 
    client->SendIRC("PRIVMSG " + to + " :" + text); 
}; 

void joinCommand(std::string channel, IRCClient* client) 
{ 
    if (channel[0] != '#') 
     channel = "#" + channel; 

    client->SendIRC("JOIN " + channel); 
} 

void partCommand(std::string channel, IRCClient* client) 
{ 
    if (channel[0] != '#') 
     channel = "#" + channel; 

    client->SendIRC("PART " + channel); 
} 

void ctcpCommand(std::string arguments, IRCClient* client) 
{ 
    std::string to = arguments.substr(0, arguments.find(" ")); 
    std::string text = arguments.substr(arguments.find(" ") + 1); 

    std::transform(text.begin(), text.end(), text.begin(), towupper); 

    client->SendIRC("PRIVMSG " + to + " :\001" + text + "\001"); 
} 

ThreadReturn inputThread(void* client) 
{ 
    std::string command; 

    commandHandler.AddCommand("msg", 2, &msgCommand); 
    commandHandler.AddCommand("join", 1, &joinCommand); 
    commandHandler.AddCommand("part", 1, &partCommand); 
    commandHandler.AddCommand("ctcp", 2, &ctcpCommand); 

    while (true) 
    { 
     getline(std::cin, command); 
     if (command == "") 
      continue; 

     if (command[0] == '/') 
      commandHandler.ParseCommand(command, (IRCClient*)client); 
     else 
      ((IRCClient*)client)->SendIRC(command); 

     if (command == "quit") 
      break; 
    } 

#ifdef _WIN32 
    _endthread(); 
#else 
    pthread_exit(NULL); 
#endif 
} 

irc.h

#pragma once 
#include <iostream> 
#include <signal.h> 
#include <cstdlib> 
#include <map> 
#include <algorithm> 
#include "Magic.h" 
#include <msclr/marshal.h> 
#include <msclr/marshal_cppstd.h> 
#using <mscorlib.dll> 


namespace IRCclient { 

    using namespace System; 
    using namespace System::ComponentModel; 
    using namespace System::Collections; 
    using namespace System::Windows::Forms; 
    using namespace System::Data; 
    using namespace System::Drawing; 
    using namespace System::IO; 
    using namespace System::Runtime::InteropServices; 
    using namespace msclr::interop; 


    /// <summary> 
    /// Summary for MyForm 
    /// </summary> 


    private: System::Void connect_button_Click(System::Object^ sender, System::EventArgs^ e) 
    { 
     if ((server_box->Text == "") || (port_box->Text == "") || (username_box->Text == "") || (channel_box->Text == "")) 
     { 
      MessageBox::Show("Wypełnij wszystkie pola", "Puste pola", MessageBoxButtons::OK, MessageBoxIcon::Warning); 
      server_box->Focus(); 
     } 
     else 
     { 
      String^ host_string = server_box->Text; 
      char* host = (char*)(void*)Marshal::StringToHGlobalAnsi(host_string); 
      String^ port_string = port_box->Text; 
      int port; 
      //String^ port_string = port.ToString(); 
      String^ nick_string = username_box->Text; 
      std::string nick(marshal_as<std::string>(nick_string)); 
      std::string user = "test"; 


      IRCClient client; 
      volatile bool running; 
      Thread thread; 
      thread.Start(&inputThread, &client); 

      if (client.InitSocket()) 
      { 

       content_box->Text = "Socket initialized. Connecting..." + "\r\n"; 

       if (client.Connect(host, port)) 
       { 
        content_box->Text = "Connected. Loggin in..." + "\r\n";     

        if (client.Login(nick, user)) 
        { 
         content_box->Text = "Logged." + "\r\n";      

         running = true; 
         signal(SIGINT, signalHandler); 

         while (client.Connected() && running) 
          client.ReceiveData(); 
        } 

        if (client.Connected()) 
         client.Disconnect(); 

        content_box->Text = "Disconnected." + "\r\n"; 
       } 
      } 
     } 
    }; 
    }; 
}; 
+1

ご質問はありますか? – Elyasin

+0

あなたの質問とREDUCEコードをMVCEに編集してください。http://stackoverflow.com/help/mcveを参照してください。 – kebs

+0

ヘッダーファイルの 'ConsoleCommandHandler commandHandler;'がオブジェクトを宣言しています。コンパイラが正しく指摘したように、このヘッダーを複数のソースファイルに組み込むと、再定義が行われます。 –

答えて

1

この変更を行う:

extern ConsoleCommandHandler commandHandler; 

その後Magic.cppファイルにこのコードを追加します。

ConsoleCommandHandler commandHandler; 

それ以外の場合、ヘッダーは各objファイルに1回ずつ2回予約されるため、Magic.objとIRC.objの両方はConsoleCommandHandler commandHandlerになります。

+0

それは助けませんでした:( – titans

+0

@titans、私は答えを更新しました。 'ConsoleCommandHandler commandHandle'を' extern'と宣言し、 'ConsoleCommandHandler commandHandle'をcppに追加してください:-) – keith

+0

magic.cppで使用されているmagic.hで定義されているvolitale boolですが、irc.hで再定義されている理由はありません。 – titans