gpt4 book ai didi

C++ 错误 : Does not name a type, 模板化类

转载 作者:行者123 更新时间:2023-11-30 02:22:19 25 4
gpt4 key购买 nike

我正在命名空间中设计一个模板化类,但我不明白为什么会出现此错误:“测试”未命名类型。它似乎只给我构造函数和析构函数定义的错误。任何帮助将不胜感激。

注意:我正在为类作业执行此操作,并且必须按照我的方式设置这些文件。我必须有一个 test.h 文件和 test.hpp 包含在 test.h 中。

测试.h:

#ifndef TEST_H
#define TEST_H

namespace mynamespace
{
template <typename A, typename B>
class Test
{
public:

// constructor
Test();

// destructor
~Test();
};
}

#include "test.hpp"

#endif

测试.hpp:

// constructor
template <typename A, typename B>
Test<A, B>::Test()
{

}

// destructor
template <typename A, typename B>
Test<A, B>::~Test()
{

}

主要.cpp:

#include "test.h"

using namespace mynamespace;

int main()
{
return 0;
}

生成文件:

CC = g++ -std=c++11 -Wall -pedantic

main.x: main.o
$(CC) -o main.x main.o

main.o: main.cpp test.h
$(CC) -c main.cpp

clean:
rm *.o *.x

最佳答案

你的实现文件没有指定正确的命名空间,所以他们找不到 mynamespace::Test。如果您更改它以包含命名空间,它将起作用:

template <typename A, typename B>
mynamespace::Test<A, B>::Test()
{

}

// destructor
template <typename A, typename B>
mynamespace::Test<A, B>::~Test()
{

}

关于C++ 错误 : Does not name a type, 模板化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47360801/

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