2013-04-27 33 views
1

我已閱讀了過去1小時內未解決的外部符號錯誤,並嘗試解決我的錯誤,但我認爲現在是時候讓我有一個全新的眼睛看看這個。無法解析的外部符號重複,但請協助

我正在使用來自Interactivebrokers.com的API在QT中構建​​交易程序。

API有我從一類EWrapperSubclass

繼承了我所要求的語法EWrapperSubclass::MethodEWrapperSubclass.cpp定義的一切,以確保Method指的是類的方法的虛類EWrapper

在我的IBProject.cpp中,我有一個簡單的EWrapper變量指向一個new EWrapperSubclass。這是發生未解析的外部符號錯誤的地方。我得到的錯誤:

LNK2019: unresolved external symbol "public: __cdecl EWrapperSubclass::EWrapperSubclass(void)" ([email protected]@[email protected]) referenced in function "public: __cdecl IBProject::IBProject(class QWidget *)" ([email protected]@[email protected]@@@Z)

可有人請這麼好心地告訴我,我可以被做錯了什麼?

EWrapperSubclass.h

#ifndef EWRAPPERSUBCLASS_H 
#define EWRAPPERSUBCLASS_H 
#include "Shared/EWrapper.h" 
class EClientSocket; 
class EWrapperSubclass : public EWrapper 
{ 
public: 
    EWrapperSubclass(); 
    ~EWrapperSubclass(); 
    EClientSocket *pEClientSocket; 

    ...//various methods declared here with void methodname 
} 

EWrapperSubclass.cpp

#include "Shared/EWrapper.h" 
#include "ewrappersubclass.h" 
#include "SocketClient/src/EClientSocket.h" 

EWrapperSubclass::EWrapperSubclass() 
{ 
    pEClientSocket = new EClientSocket(this); 
    pEClientSocket->eConnect("127.0.0.1",4001,0); 
    connect(this,SIGNAL(disconnected()),this,SLOT(reconnect()); 
} 

EWrapperSubclass::~EWrapperSubclass(){ 
    pEClientSocket->eDisconnect(); 
    delete pEClientSocket; 
} 

void EWrapperSubclass::isConnected(){ 
    return pEClientSocket->isConnected(); 
} 

...//various methods defined here as void EWrapperSubclass::Methodname 

IBProject.h

#ifndef IBPROJECT_H 
#define IBPROJECT_H 

#include <QMainWindow> 
#include "Shared/EWrapper.h" 
#include "ewrappersubclass.h" 


namespace Ui { 
    class IBProject; 
} 

class EWrapper; 
class EWrapperSubclass; 
class IBProject : public QMainWindow 
{ 
Q_OBJECT 

public: 
    explicit IBProject(QWidget *parent = 0); 
    ~IBProject(); 

private: 
    Ui::IBProject *ui; 
    EWrapper *pewrapper; 
}; 

#endif // IBPROJECT_H 

IBproject.cpp

#include "ibproject.h" 
#include "ui_ibproject.h" 

IBProject::IBProject(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::IBProject) 
{ 
    ui->setupUi(this); 

    pewrapper = new EWrapperSubclass; 

    connect(ui->tradeButton,SIGNAL(clicked()),this,SLOT(on_tradeButton_clicked())); 
    connect(pewrapper,SIGNAL(connected()),this,SLOT(on_connected)); 
    connect(pewrapper,SIGNAL(disconnected()),this,SLOT(on_disconnected)); 
} 

IBProject::~IBProject() 
{ 
    delete ui; 
    delete pewrapper; 
} 
+0

和文件'EWrapperSubclass.cpp'是正確編譯並與項目的其餘部分相關聯? – 2013-04-27 17:22:44

+0

@JoachimPileborg正是我在想什麼...... Chowza:你如何處理Microsoft Visual C++中的項目依賴關係? – Sebivor 2013-04-27 17:25:04

+0

@JoachimPileborg我會這麼認爲,我該如何檢查? (我正在開發QT ide) – 2013-04-27 17:25:22

回答

0

PosixTestClient.h

#ifndef posixtestclient_h__INCLUDED 
#define posixtestclient_h__INCLUDED 

#include "EWrapper.h" 

#include <memory> 
#include <stdio.h> //printf() 

namespace IB { 

class EPosixClientSocket; 

enum State { 
    ST_CONNECT, 
    ST_PLACEORDER, 
    ST_PLACEORDER_ACK, 
    ST_CANCELORDER, 
    ST_CANCELORDER_ACK, 
    ST_PING, 
    ST_PING_ACK, 
    ST_IDLE 
}; 


class PosixTestClient : public EWrapper 
{ 
public: 

    PosixTestClient(); 
    ~PosixTestClient(); 

    void processMessages(); 

public: 

    bool connect(const char * host, unsigned int port, int clientId = 0); 
    void disconnect() const; 
    bool isConnected() const; 

private: 

    void reqCurrentTime(); 
    void placeOrder(); 
    void cancelOrder(); 

public: 
    // events 
    void tickPrice(TickerId tickerId, TickType field, double price, int canAutoExecute); 
    void tickSize(TickerId tickerId, TickType field, int size); 
    void tickOptionComputation(TickerId tickerId, TickType tickType, double impliedVol, double delta, 
     double optPrice, double pvDividend, double gamma, double vega, double theta, double undPrice); 
    void tickGeneric(TickerId tickerId, TickType tickType, double value); 
    void tickString(TickerId tickerId, TickType tickType, const IBString& value); 
//... etc 

private: 

    std::auto_ptr<EPosixClientSocket> m_pClient; 
    State m_state; 
    time_t m_sleepDeadline; 

    OrderId m_orderId; 
}; 

}

實施,PosixTestClient.cpp

#include "PosixTestClient.h" 

#include "EPosixClientSocket.h" 
/* In this example we just include the platform header to have select(). In real 
    life you should include the needed headers from your system. */ 
#include "EPosixClientSocketPlatform.h" 

#include "Contract.h" 
#include "Order.h" 

#include <time.h> 
#include <sys/time.h> 

#if defined __INTEL_COMPILER 
# pragma warning (disable:869) 
#elif defined __GNUC__ 
# pragma GCC diagnostic ignored "-Wswitch" 
# pragma GCC diagnostic ignored "-Wunused-parameter" 
#endif /* __INTEL_COMPILER */ 

namespace IB { 

const int PING_DEADLINE = 2; // seconds 
const int SLEEP_BETWEEN_PINGS = 30; // seconds 

/////////////////////////////////////////////////////////// 
// member funcs 
PosixTestClient::PosixTestClient() 
    : m_pClient(new EPosixClientSocket(this)) 
    , m_state(ST_CONNECT) 
    , m_sleepDeadline(0) 
    , m_orderId(0) 
{ 
} 

PosixTestClient::~PosixTestClient() 
{ 
} 

bool PosixTestClient::connect(const char *host, unsigned int port, int clientId) 
{ 
    // trying to connect 
    printf("Connecting to %s:%u clientId:%d\n", !(host && *host) ? "127.0.0.1" : host, port, clientId); 

    bool bRes = m_pClient->eConnect2(host, port, clientId); 

    if (bRes) { 
     printf("Connected to %s:%u clientId:%d\n", !(host && *host) ? "127.0.0.1" : host, port, clientId); 
    } 
    else 
     printf("Cannot connect to %s:%u clientId:%d\n", !(host && *host) ? "127.0.0.1" : host, port, clientId); 

    return bRes; 
} 

void PosixTestClient::disconnect() const 
{ 
    m_pClient->eDisconnect(); 

    printf ("Disconnected\n"); 
} 

bool PosixTestClient::isConnected() const 
{ 
    return m_pClient->isConnected(); 
} 

void PosixTestClient::processMessages() 
{ 
//... 
} 

////////////////////////////////////////////////////////////////// 
// methods 
void PosixTestClient::reqCurrentTime() 
{ 
    printf("--> Requesting Current Time2\n"); 

    // set ping deadline to "now + n seconds" 
    m_sleepDeadline = time(NULL) + PING_DEADLINE; 

    m_state = ST_PING_ACK; 

    m_pClient->reqCurrentTime(); 
} 

//...etc 

/////////////////////////////////////////////////////////////////// 
// events 
void PosixTestClient::orderStatus(OrderId orderId, const IBString &status, int filled, 
     int remaining, double avgFillPrice, int permId, int parentId, 
     double lastFillPrice, int clientId, const IBString& whyHeld) 

{} 

void PosixTestClient::error(const int id, const int errorCode, const IBString errorString) 
{ 
    printf("Error id=%d, errorCode=%d, msg=%s\n", id, errorCode, errorString.c_str()); 

    if(id == -1 && errorCode == 1100) // if "Connectivity between IB and TWS has been lost" 
     disconnect(); 
} 

void PosixTestClient::tickPrice(TickerId tickerId, TickType field, double price, int canAutoExecute) {} 
void PosixTestClient::tickSize(TickerId tickerId, TickType field, int size) {} 
void PosixTestClient::tickOptionComputation(TickerId tickerId, TickType tickType, double impliedVol, double delta, 
              double optPrice, double pvDividend, 
              double gamma, double vega, double theta, double undPrice) {} 
void PosixTestClient::tickGeneric(TickerId tickerId, TickType tickType, double value) {} 
void PosixTestClient::tickString(TickerId tickerId, TickType tickType, const IBString& value) {} 
void PosixTestClient::tickEFP(TickerId tickerId, TickType tickType, double basisPoints, const IBString& formattedBasisPoints, 
           double totalDividends, int holdDays, const IBString& futureExpiry, double dividendImpact, double dividendsToExpiry) {} 
void PosixTestClient::openOrder(OrderId orderId, const Contract&, const Order&, const OrderState& ostate) {} 
void PosixTestClient::openOrderEnd() {} 
void PosixTestClient::winError(const IBString &str, int lastError) {} 
void PosixTestClient::connectionClosed() {} 
void PosixTestClient::updateAccountValue(const IBString& key, const IBString& val, 
              const IBString& currency, const IBString& accountName) {} 
void PosixTestClient::updatePortfolio(const Contract& contract, int position, 
     double marketPrice, double marketValue, double averageCost, 
     double unrealizedPNL, double realizedPNL, const IBString& accountName){} 
void PosixTestClient::updateAccountTime(const IBString& timeStamp) {} 
void PosixTestClient::accountDownloadEnd(const IBString& accountName) {} 
void PosixTestClient::contractDetails(int reqId, const ContractDetails& contractDetails) {} 
void PosixTestClient::bondContractDetails(int reqId, const ContractDetails& contractDetails) {} 
void PosixTestClient::contractDetailsEnd(int reqId) {} 
void PosixTestClient::execDetails(int reqId, const Contract& contract, const Execution& execution) {} 
void PosixTestClient::execDetailsEnd(int reqId) {} 

void PosixTestClient::updateMktDepth(TickerId id, int position, int operation, int side, 
             double price, int size) {} 
void PosixTestClient::updateMktDepthL2(TickerId id, int position, IBString marketMaker, int operation, 
             int side, double price, int size) {} 
void PosixTestClient::updateNewsBulletin(int msgId, int msgType, const IBString& newsMessage, const IBString& originExch) {} 
void PosixTestClient::managedAccounts(const IBString& accountsList) {} 
void PosixTestClient::receiveFA(faDataType pFaDataType, const IBString& cxml) {} 
void PosixTestClient::historicalData(TickerId reqId, const IBString& date, double open, double high, 
             double low, double close, int volume, int barCount, double WAP, int hasGaps) {} 
void PosixTestClient::scannerParameters(const IBString &xml) {} 
void PosixTestClient::scannerData(int reqId, int rank, const ContractDetails &contractDetails, 
     const IBString &distance, const IBString &benchmark, const IBString &projection, 
     const IBString &legsStr) {} 
void PosixTestClient::scannerDataEnd(int reqId) {} 
void PosixTestClient::realtimeBar(TickerId reqId, long time, double open, double high, double low, double close, 
            long volume, double wap, int count) {} 
void PosixTestClient::fundamentalData(TickerId reqId, const IBString& data) {} 
void PosixTestClient::deltaNeutralValidation(int reqId, const UnderComp& underComp) {} 
void PosixTestClient::tickSnapshotEnd(int reqId) {} 
void PosixTestClient::marketDataType(TickerId reqId, int marketDataType) {} 

} 

,並在主要使用:

#ifdef _WIN32 
# include <windows.h> 
# define sleep(seconds) Sleep(seconds * 1000); 
#else 
# include <unistd.h> 
#endif 

#include "PosixTestClient.h" 

const unsigned MAX_ATTEMPTS = 1; 
const unsigned SLEEP_TIME = 10; 

int main(int argc, char** argv) 
{ 
    IB::PosixTestClient client; 
     client.connect(host, port, clientId); 
     //... 
}