C++ 为什么我的simpleVector.cpp没有链接?(从c+;+;开始)
我直接从书中得到了.cpp和.h,似乎什么都没有遗漏,但我得到了这些链接错误C++ 为什么我的simpleVector.cpp没有链接?(从c+;+;开始),c++,vector,C++,Vector,我直接从书中得到了.cpp和.h,似乎什么都没有遗漏,但我得到了这些链接错误 #ifndef SIMPLEVECTOR_H #define SIMPLEVECTOR_H #include <iostream> #include <new> // needed for bad__alloc exception #include <cstdlib> // needed for the exit function using namesp
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream>
#include <new> // needed for bad__alloc exception
#include <cstdlib> // needed for the exit function
using namespace std;
template <class T>
class simplevector
{
private:
T *aptr;
int arraysize;
void memerror(); // handles mem aloc errors
void suberror(); // handles subscripts out of range
public:
simplevector() // default constructor
{ aptr = 0; arraysize = 0; }
simplevector(int); // constructor
simplevector(const simplevector &); // coppy constructor
~simplevector();
int size()
{ return arraysize; }
T &operator[](const int &); // overloaded [] operator
};
template <class T>
simplevector<T>::simplevector(int s)
{
arraysize = s;
// allocate memory for the array
try
{
aptr = new T [s];
}
catch (bad_alloc)
{
memerror();
}
// initialize the array.
for (int count = 0; count < arraysize; count++)
*(aptr + count) = 0;
}
template <class T>
simplevector<T>::simplevector(const simplevector &obj)
{
arraysize = obj.arraysize;
aptr = new T [arraysize];
if (aptr == 0)
memerror();
for(int count = 0; count < arraysize; count++)
*(aptr + count) = *(obj.aptr + count);
}
template <class T>
simplevector<T>::~simplevector()
{
if (arraysize > 0)
delete [] aptr;
}
template <class T>
void simplevector<T>::memerror()
{
cout << "ERROR: Cannot allocate memory.\n";
exit(0);
}
template <class T>
T &simplevector<T>::operator [](const int &sub)
{
if (sub < 0 || sub >= arraysize)
suberror();
return aptr[sub];
}
#endif
\ifndef SIMPLEVECTOR\u H
#定义SIMPLEVECTOR_H
#包括
#包含//错误分配异常所需
#包含退出功能所需的//项
使用名称空间std;
模板
类simplevector
{
私人:
T*aptr;
内部阵列化;
void memerror();//处理内存错误
void suberror();//处理的下标超出范围
公众:
simplevector()//默认构造函数
{aptr=0;arraysize=0;}
simplevector(int);//构造函数
simplevector(const simplevector&);//copy构造函数
~simplevector();
int size()
{返回数组化;}
T运算符[](常量int&);//重载的[]运算符(&O)
};
模板
simplevector::simplevector(int s)
{
arraysize=s;
//为阵列分配内存
尝试
{
aptr=新的T[s];
}
捕获(错误分配)
{
memerror();
}
//初始化数组。
for(int count=0;count0)
删除[]aptr;
}
模板
void simplevector::memerror()
{
cout=阵列化)
子错误();
返回aptr[sub];
}
#恩迪夫
此程序演示simplevector模板:
#include <iostream>
#include "simplevector.h"
using namespace std;
int main()
{
simplevector<int> inttable(10);
simplevector<float> floattable(10);
int x;
//store values in the arrays.
for (x = 0; x < 10; x++)
{
inttable[x] = (x * 2);
floattable[x] = (x * 2.14);
}
//display the values in the arrays.
cout << "these values are in inttable:\n";
for (x = 0; x< 10; x++)
cout << inttable[x] << " ";
cout << endl;
cout << "these values are in floattable:\n";
for (x = 0; x< 10; x++)
cout << floattable[x] << " ";
cout << endl;
//use the standard + operator on array elements.
cout << "\nAdding 5 to each element of inttable"
<< " and floattable.\n";
for (x = 0; x< 10; x++)
{
inttable[x] = inttable[x] + 5;
floattable[x] = floattable[x] + 1.5;
}
//display the values in the array.
cout << "These values are in inttable:\n";
for (x = 0; x< 10; x++)
cout << inttable[x] << " ";
cout << endl;
cout << "These values are in floattable:\n";
for (x = 0; x< 10; x++)
cout << floattable[x] << " ";
cout << endl;
// use the standard ++ operator on array elements.
cout << "\nIncrementing each element of inttable and"
<< " floattable.\n";
for (x = 0; x< 10; x++)
{
inttable[x]++;
floattable[x]++;
}
//display the values in the array.
cout << "These values are in inttable:\n";
for (x = 0; x< 10; x++)
cout << inttable[x] << " ";
cout << endl;
cout << "These values are in floattable:\n";
for (x = 0; x< 10; x++)
cout << floattable[x] << " ";
cout << endl;
return 0;
}
#包括
#包括“simplevector.h”
使用名称空间std;
int main()
{
simplevector inttable(10);
simplevector浮动台(10);
int x;
//将值存储在数组中。
对于(x=0;x<10;x++)
{
inttable[x]=(x*2);
可浮动[x]=(x*2.14);
}
//显示数组中的值。
它就在你在OP评论中发布的错误中——你声明了simplevector::suberror
,但没有定义它。suberror()的核心在哪里?我在类实现中没有看到这一点。定义“这些链接错误…”。为什么不粘贴编译器输出。这些链接错误可能是什么?我在粘贴栏中没有看到任何链接错误“但是我得到了这些链接错误…”其中?1>simplevector.obj:error LNK2019:unresolved external symbol“private:void\uu thiscall simplevector::suberror(void)”(?suberror@$simplevector@H@@AAEXXZ)在函数中引用“public:int&u thiscall simplevector::operator[](int const&)”(?A$simplevector@H@@QAEAAHABH@Z)1> simplevector.obj:错误LNK2019:未解析的外部符号“private:void”\uu thiscall simplevector::suberror(void)”(?suberror@$simplevector@M@@AAEXXZ)在函数“public:float&u thiscall simplevector::operator[](int const&)中引用(?)A$simplevector@M@@QAEAAMABH@Z)