Catch2 框架错误:未知类型名称“DNA”clang-tidy(clang-diagnostic-error)

发布于 2025-01-16 03:36:21 字数 855 浏览 1 评论 0原文

我正在使用 catch2 框架进行单元测试。下面是我用来编译测试文件的命令。

bin/tests: ./tests/tests.cc ./src/dna_strand.cc ./includes/dna_strand.hpp $(CXX) $(CXXFLAGS) ./tests/tests.cc ./src/dna_strand.cc -o $@

我正在尝试初始化构造函数。我正在尝试使用类的方法,但它不起作用。我猜我的包含有问题。 我还尝试初始化我的 DNAstrand 类,但它一直抛出相同的错误。

下面是代码:

#ifndef CATCH_CONFIG_MAIN
#  define CATCH_CONFIG_MAIN
#endif


#include <catch/catch.hpp>
#include <iostream>

#include "dna_strand.hpp"
#include "node.hpp"

DNAstrand sample();
sample.PushNode('h'); // <- error

DNAstrand\* DNA = new DNAstrand();
DNA->SpliceIn('h'); // <- error

TEST_CASE("Does nothing", "\[does-nothing\]") { REQUIRE(true == true); 

下面是我得到的 4 个错误

unknown type name 'sample'
cannot use dot operator on a type
unknown type name 'DNA'
cannot use arrow operator on a type

I'm using catch2 framework for unit testing. Below is the command I use for compiling the test file.

bin/tests: ./tests/tests.cc ./src/dna_strand.cc ./includes/dna_strand.hpp $(CXX) $(CXXFLAGS) ./tests/tests.cc ./src/dna_strand.cc -o $@

I'm trying to initialize constructor. I'm trying to use the class' method but it does not work. I'm guessing there is something wrong with my includes.
I also tried to initialize my DNAstrand class but it keeps throwing the same error.

Below is the code:

#ifndef CATCH_CONFIG_MAIN
#  define CATCH_CONFIG_MAIN
#endif


#include <catch/catch.hpp>
#include <iostream>

#include "dna_strand.hpp"
#include "node.hpp"

DNAstrand sample();
sample.PushNode('h'); // <- error

DNAstrand\* DNA = new DNAstrand();
DNA->SpliceIn('h'); // <- error

TEST_CASE("Does nothing", "\[does-nothing\]") { REQUIRE(true == true); 

Belows are the 4 errors I get

unknown type name 'sample'
cannot use dot operator on a type
unknown type name 'DNA'
cannot use arrow operator on a type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴梦长久 2025-01-23 03:36:21

C++ 不允许在函数之外使用代码(不包括定义和声明),这意味着您需要将代码移至 TEST_CASE 宏内部。

C++ does not allow code outside of functions (excluding definitions and declarations), meaning you need to move your code inside the TEST_CASE macro.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文