1

array_T汎用配列であるテンプレートクラスとテンプレート関数linear search TEMPLATE.hがあり、クラスのフレンドとして宣言されているため、クラスのメンバー関数と動的割り当て配列であるクラスのデータメンバーarrayTemplate.h を使用できますgetArraySize()a

arrayTemplate.h

    #ifndef arrayTemplate
    #define arrayTemplate
    #include"linear search TEMPLATE.h"
    #include<iostream>
    using namespace std;

    template <class T>
class array_T  {

private:
    T *a;
    int arraySize;

public : 
    friend void linearSearch(array_T object);

    array_T(int s) {
        arraySize = s;
        a = new T[arraySize];

        for (int i= 0; i < arraySize; ++i) {
            a[i] = 0;
        }
    }

    ~array_T() {
        delete[]a;
    }

    void setArray() {
        for (int  i=0; i < arraySize; ++i) {
            cout << "Enter the elements of the array " << endl;
            cin >> a[i];
        }
    }

    void getArray() {
        for (int  i=0; i < arraySize; ++i) {
            cout << a[i] << endl;
        }
    }

    int getArraySize() {
        return arraySize;
    }


};
#endif

線形検索 TEMPLATE.h

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

template <class T>
//void linearSearch(T desiredData, int arraySize, T *elemnts) {
void linearSearch(array_T<T> object , T desiredData) {
    int arraySize = object.getArraySize();
    int loc = -1;
    int i = 0;
    for (i = 0; i < arraySize; ++i) {

        if (object.a[i] == desiredData) {
            loc = i;
            break;
        }
    }

    if (loc > 0) {
        cout << "the Item is found at position " << i + 1 << endl;
    }
    else {
        cout << "the item is not found ";
    }

}

main.cpp

  #include"arrayTemplate.h"
   #include"linear search TEMPLATE.h"
    #include<iostream>
    using namespace std;



int main() {

    array_T<int> myArray(7);
    myArray.setArray();

    linearSearch(myArray,50)

return 0 ; 
}

これらは私が得たエラーです

線形検索 template.h(8): エラー C2065: 'array_T': 宣言されていない識別子

線形検索 template.h(8): エラー C2065: 'オブジェクト': 宣言されていない識別子

線形検索 template.h(8): エラー C2275: 'T': この型を式として不正に使用しています

線形検索 template.h(6): 注: 'T' の宣言を参照してください

線形検索 template.h(8): エラー C2146: 構文エラー: 識別子 'desiredData' の前に ')' がありません

main.cpp(14): エラー C2660: 'linearSearch': 関数は 2 つの引数を取りません

========== ビルド: 0 成功、1 失敗、0 最新、0 スキップ ==========

4

0 に答える 0