gpt4 book ai didi

c++ - 错误 : constructor does not name a type

转载 作者:太空宇宙 更新时间:2023-11-04 11:49:38 25 4
gpt4 key购买 nike

所以现在我已经设置了一个非常基本的类 cnt。即返回错误

error: 'Cnt' does not name a type

对于 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;
}

最佳答案

在模板元编程中,你的声明和定义应该在同一个头文件中,

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

关于c++ - 错误 : constructor does not name a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18886263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com