2017-11-25 14 views
0

私は4つのファイルで構成されたコードプロジェクト、との問題を見つけるのひどい時間を持っている:「PreconditionViolatedException :: PreconditionViolatedException(STD ::文字列のconst&)」への未定義参照

GiftRegistry.cpp:

#include "GiftRegistry.h" 
#include "LinkList/LinkedList.h" 
#include "LinkList/LinkedList.cpp" 
#include <iostream> 

template<class ItemType> 
GiftRegistry<ItemType>::GiftRegistry() : giftRegistry(new LinkedList<ItemType>()) 
{ 
} 

template<class ItemType> 
bool GiftRegistry<ItemType>::addGift(const ItemType& gift, int desiredRank) 
{ 
    if(giftRegistry->insert(desiredRank, gift)) 
    { 
     return true; 
    } 
    else{ 
     return false; 
    } 
} 

template<class ItemType> 
void GiftRegistry<ItemType>::moveGift(int curPos, int newPos) 
{ 
    if(giftRegistry->isEmpty()) 
    { 
     ItemType itemToMove = giftRegistry->getEntry(curPos); 

     if(giftRegistry->insert(newPos, itemToMove)) 
     { 
      cout << "Gift moved successfully!"; 
     } 
     if(giftRegistry->remove(curPos)) 
     { 
      cout << "Gift removed successfully"; 
     } 
     else{ 
      cout << "Operation failed. Please examine input."; 
     } 
    } 
} 

template<class ItemType> 
bool GiftRegistry<ItemType>::removeGift(int position) 
{ 
    if(giftRegistry->isEmpty()) 
    { 
     if(giftRegistry->remove(position)) 
     { 
      return true; 
     } 

    } 
    else 
    { 
     return false; 
    } 
} 

template<class ItemType> 
void GiftRegistry<ItemType>::showRegistry() 
{ 
    for(int i = 1; i < giftRegistry->getLength(); i++) 
    { 
     ItemType itemToPrint = giftRegistry->getEntry(i); 
     std::cout << i + "." + itemToPrint; 
    } 
} 

main.cppに:

#include "GiftRegistry.cpp" 
//#include "LinkList/LinkedList.cpp" 
#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 
    ListInterface<string>* listPtr = new LinkedList<string>(); 
    //RegistryInterface<string>* weddingRegistry = new GiftRegistry<string>(); 
    string gift = "hello there"; 
    listPtr->insert(1, gift); 
    cout << listPtr->getEntry(1); 
// string gift = "Box of rocks"; 
// 
// RegistryInterface<string>* weddingRegistry = new GiftRegistry<string>(); 
// if(weddingRegistry->addGift(gift, 1)) 
// { 
//  cout << "Added item " + gift; 
//  weddingRegistry->showRegistry(); 
// } 
// else 
// { 
//  cout << "Failed to add item. Error."; 
// } 
    //weddingRegistry->showRegistry(); 

    return 0; 
} 

GiftRegistry.h:

#ifndef Gift_Registry_ 
#define Gift_Registry_ 

#include "RegistryInterface.h" 
#include "C:\Users\Austin\Documents\LinkList\LinkedList.h" 
using namespace std; 

template<class ItemType> 
class GiftRegistry : public RegistryInterface<ItemType> 
{ 
private: 
    LinkedList<ItemType>* giftRegistry; 

public: 
    string CustomerName; 
    string EventName; 
    GiftRegistry(); 

    bool addGift(const ItemType& gift, int desiredRank); 
    void moveGift(int curPos, int newPos); 
    bool removeGift(int position); 
    void showRegistry(); 

    virtual ~GiftRegistry(); 
}; 

#endif // _GiftRegistry_ 

RegistryInterface.h:

#ifndef REGISTRY_INTERFACE_ 
#define REGISTRY_INTERFACE_ 

template<class ItemType> 
class RegistryInterface 
{ 
public: 
    /** Adds gift to the gift registry 
    @post Add was successful 
    @param gift The gift to be added to the registry. 
    @param giftRank The rank to be attributed to the gift. Also determines the point of insertion for the gift. 
    @return 'True' if addition was successful, 'false' if item already exists in the list 
    **/ 
    virtual bool addGift(const ItemType& gift, int giftRank) = 0; 

    /** Moves the gift to the user-desired location/priority 
    @pre List is not empty 
    @param currentPosition The current position of the gift to be moved. 
    @param desiredPosition The new position 
    **/ 
    virtual void moveGift(int currentPosition, int desiredPosition) = 0; 

    /** Removes the specified item from the List 
    @pre List is not empty 
    @post Item was removed 
    @param position The index of the item to be removed 
    @return 'True' if gift was removed, 'False' if not 
    **/ 
    virtual bool removeGift(int position) = 0; 


    /** Prints the registry contents and displays them by most wanted to least wanted 
    @pre List is not empty 
    **/ 
    virtual void showRegistry() = 0; 

    /** Destroys this registry and frees its assigned memory. 
    **/ 
    virtual ~RegistryInterface() { } 
}; 
#endif // REGISTRY_INTERFACE_ 

私はコード::ブロック16.01を使用してC++ 11の標準に準拠しています。

|| ===ビルド:GiftListでのデバッグ(コンパイラ:GNU GCCコンパイラ)| ===次のエラーが表示されるようになり「main.cppに」ファイルを、実行しているときに問題が生じます obj \ Debug \ main.o ||関数ZNK10LinkedListISsE8getEntryEi':| C:\Users\Arralan\Documents\LinkList\LinkedList.cpp|121|undefined reference to PreconditionViolatedException :: PreconditionViolatedException(std :: string const &) '| obj \ Debug \ main.o ||関数ZN10LinkedListISsE7replaceEiRKSs':| C:\Users\Arralan\Documents\LinkList\LinkedList.cpp|140|undefined reference to PreconditionViolatedException :: PreconditionViolatedException(std :: string const &) '| ||エラー:ldは1の終了ステータスを返しました。 || ===構築に失敗しました:3つのエラー、0の警告(0分、0秒)=== |

私はData-Structuresのnew-ishです。この問題は、今では2日間抱きました。どんな助けや指導も頂ければ幸いですが、これは宿題です。

"LinkList"のコードはエラーがなく、別のプロジェクトで意図したとおりに動作しますが、別のプロジェクトからの参照であると不思議な動作をします。問題は私の '#include'ステートメントにあると信じています。

EDIT:この問題は、この一意のインスタンスで必要だった「#include」によって引き起こされました。最良かどうかにかかわらず、私が見つけた解決策は、このパスを '#include': "LinkList/PreconditionViolatedException.cpp"で追加することです。私の 'main.cpp'にこの行を追加した後、エラーは表示されなくなりました。

+3

[定義されていない参照/未解決の外部シンボルエラーとは何ですか?どうすれば修正できますか?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-外部シンボルエラーとその対応方法) –

+1

テンプレート定義はヘッダファイルに格納されています。 –

答えて

0

この問題は、この一意のインスタンスで必要だった「#include」が原因でした。最良かどうかにかかわらず、私が見つけた解決策は、このパスを '#include': "LinkList/PreconditionViolatedException.cpp"で追加することです。私の 'main.cpp'にこの行を追加した後、エラーは表示されなくなりました。

関連する問題