0

Tの配列であるコンテナである非常に単純なテンプレートがあります。構文エラーが発生しています:

container.h(7): エラー C2143: 構文エラー: ';' がありません 前 '&'。そこで宣言を削除しようとしましたが、エラーは定義にスキップします。助けていただければ幸いです。

編集: 名前空間の使用を修正しましたが、別のエラーが発生しました: container.h(8): エラー C2975: 'Container': 'unnamed-parameter' の無効なテンプレート引数、予期されるコンパイル時の定数式

#include <typeinfo.h>
#include <assert.h>
#include <iostream>
#pragma once

using namespace std; 
template <typename T, int> class Container;
template <typename T, int> ostream& operator<< <>(ostream &, const Container<T,int> &);

template<class T , int capacity=0> class Container
{
    //using namespace std;
private:
    T inside[capacity];
public:
    Container()
    {

    }

    ~Container(void)
    {
    }

    void set(const T &tType, int index)
    {
        assert(index>=0 && index<= capacity);
        inside[index] = tType;
    }

    T& operator[](int index)
    {
        assert(index>=0 && index<= capacity);
        return inside[index];
    }

    friend ostream& operator<< <>(ostream& out, const Container<T,int> c);
    {
        for(int i=0;i<sizeof(inside)/sizeof(T);i++)
            out<<c.inside[i]<< "\t";
        return out;
    }
};
4

2 に答える 2

4

あなたはおそらく欲しい:

template <typename T, int N>
ostream& operator<<(ostream &, const Container<T,N> &);
//                                               ^ here you need N, not int!

または、実際には前方宣言が必要ないため、クラスでこの実装を使用するだけです。

friend ostream& operator<<(ostream & out, const Container<T,capacity>& c)
{
    for(int i=0;i<capacity;++i)
        out<<c.inside[i]<< "\t";
    return out;
}
于 2013-09-11T12:09:55.940 に答える