1

だから今、私はかなり基本的なクラス cnt をセットアップしています。それはエラーを返しています

エラー: 'Cnt' は型を指定していません

cnt() と cnt(T t) の両方に対して。私の知る限り、これは私の教科書がテンプレートクラスを定義する方法と一致しているので、ここで何が間違っているのでしょうか?

cnt.h:

#ifndef CNT_H_
#define CNT_H_

#include <iostream>

template <typename T>

class Cnt
{
public:
    Cnt();
    Cnt(T t);

private:
    T item;
    int cnt;
};

#include "cnt.cpp"
#endif

cnt.cpp:

template<typename T>
Cnt<T>::Cnt()
{
  cnt = 0;
}

template<typename T>
Cnt<T>::Cnt(T t)
{
  item = t;
  cnt = 0;
}
4

2 に答える 2

0

Cntテンプレート引数なしでクラスをインスタンス化している可能性があります。と言う代わりに

Cnt c;

次のようなタイプを指定する必要があります。

Cnt<int> c;

さらに、関数をインラインで定義する必要があるため、cnt.handの代わりに、以下を含むcnt.cpp1 つのファイルが必要です。cnt.hpp

#ifndef CNT_H_
#define CNT_H_

#include <iostream>

template <typename T>
class Cnt
{
public:
    Cnt() : cnt(0) { }
    Cnt(T t) : item(t), cnt(0) { }

private:
    T item;
    int cnt;
};
#endif
于 2013-09-19T04:43:51.627 に答える
0

テンプレートのメタプログラミングでは、宣言と定義は同じヘッダー ファイルにある必要があります。

#ifndef CNT_H_
#define CNT_H_

#include <iostream>

template <typename T>

class Cnt
{
public:
    Cnt();
    Cnt(T t);

private:
    T item;
    int cnt;
};

template<typename T>
Cnt<T>::Cnt()
{
  cnt = 0;
}

template<typename T>
Cnt<T>::Cnt(T t)
{
  item = t;
  cnt = 0;
}
于 2013-09-19T04:44:02.140 に答える