2011-12-05 10 views
0

を(C++で出始め)、何が欠けていないかのように思えるが、イムは、これらのリンクエラーが発生して....なぜ私のsimpleVector.cppはリンクしていませんか?私は.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 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 

このプログラムは実証しますシンプルベクトルテンプレート:

#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; 
} 
+0

"これらのリンクエラー...."を定義します。コンパイラの出力を貼り付けてみてください。 – PeterT

+0

これらのリンクエラーは何か?私はペーストビンには何も見ません – Praetorian

+0

* "しかし、これらのリンクエラーを取得しています...." * ...どこ? – AusCBloke

答えて

1

あなたがOPコメントに投稿したエラーのところにあります。あなたはそれを定義することなくsimplevector::suberrorと宣言しました。

+0

ありがとう、私はなぜそれを見つけることができなかったのか分からない – user1080889

1

suberror()の腸はどこですか?私はクラスの実装でそれを表示されません。

関連する問題