gpt4 book ai didi

C++ 错误 LNK2019 : unresolved external symbol

转载 作者:行者123 更新时间:2023-11-30 04:18:13 32 4
gpt4 key购买 nike

<分区>

#ifndef SET_H
#define SET_H

#include <iostream>
using namespace std;

template<class ItemType>
class Set;


template <class ItemType>
class Set{
private:
ItemType *elements; // This is a sample representation
int capacity; // You are free to use other representations
int num_of_elements;
public:
Set(); // Constructor
Set(const Set<ItemType>& other); // Copy Constructor
~Set(); // Destructor

/**Member Functions**/

void addElement (const ItemType &element);
/* Add the element to the set.
If the element is already in the set DO NOT add it. */
};
#endif


#include <iostream>
#include "set.h"
using namespace std;


template <class ItemType>
Set<ItemType>::Set(){
elements = new ItemType[20];
num_of_elements = 0;
capacity = 10;
}

template <class ItemType>
Set<ItemType>::Set(const Set<ItemType>& other):
elements(other->elements), num_of_elements(other->num_of_elements), capacity(other->capacity)
{}

template <class ItemType>
Set<ItemType>::~Set(){
delete[] elements;
}

template <class ItemType>
void Set<ItemType>::addElement (const ItemType &element){
ItemType * temp = new ItemType [capacity];
for( int i = 0; i < num_of_elements ; i ++ ) {
temp[i] = elements[i];}

delete [] elements;
elements = temp;

elements[num_of_elements] = element;
num_of_elements++;
}

template <class ItemType>
bool Set<ItemType>::removeElement(const ItemType &item){
for( int i = 0; i < _size; i++ ) {
if( _items[i] == item )
}
int place = i;
if( place == -1 )
return false;

elements[place]= elements[num_of_elements-1];
num_of_elements --;
return true;
}



#include <iostream>
#include <cstdlib>
#include "set.h"

using namespace std;

int main(){

Set<int> s;
int x=8;
s.addElement(x);

for (int i=0;i<5;i++)
s.addElement(i+6);

cout << "s: " << s << endl;




system("pause");
return 0;
}



This is the code that i write but i'm getting the following error.

1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Set<int>::Set<int>(void)" (??0?$Set@H@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Set<int>::~Set<int>(void)" (??1?$Set@H@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Set<int>::addElement(int const &)" (?addElement@?$Set@H@@QAEXABH@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Set<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Set@H@@@Z) referenced in function _main

这是我编写的代码,它是关于模板的作业,但我遇到了一些烦人的错误。

我找不到错误消息的来源我认为它与模板有关我该如何解决这些问题?

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com