gpt4 book ai didi

c++ - 在 C++ 中定义未命名结构时出错

转载 作者:行者123 更新时间:2023-12-04 15:00:54 28 4
gpt4 key购买 nike

<分区>

我的代码有问题,连我的教授都不知道为什么会这样。

他要我用 using Persontype= struct{}; 定义结构,而不是结构的正常定义。我不明白为什么或有什么区别。

他也不希望我们在 main 中包含 .cpp 文件。

结构的正常定义工作正常但不适用于此。

错误

In file included from ...\LABOR1TEST\main.cpp:3:
...\LABOR1TEST\test.hpp:12:6: warning: 'void test(const std::vector<<unnamed struct> >&)' used but never defined
void test(std::vector<PersonType> const &_Personen);
^~~~
[100%] Linking CXX executable LABOR1TEST.exe
CMakeFiles\LABOR1TEST.dir/objects.a(main.cpp.obj): In function `main':
.../LABOR1TEST/main.cpp:12: undefined reference to `test(std::vector<._56, std::allocator<._56> > const&)'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\LABOR1TEST.dir\build.make:120: LABOR1TEST.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:95: CMakeFiles/LABOR1TEST.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:102: CMakeFiles/LABOR1TEST.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:137: LABOR1TEST] Error 2

这是我的代码

主要.cpp

#include <iostream>
#include <vector>
#include "test.hpp"

int main() {

PersonType p;
p.Name = "Max";
std::vector<PersonType> TEMPVEC;
TEMPVEC.push_back(p);
TEMPVEC.push_back(p);
test (TEMPVEC);
return 0;
}

测试.cpp

#include <iostream>
#include <vector>
#include <string>
#include "test.hpp"
void test(std::vector<PersonType> const &_Personen)
{
for (auto const &i : _Personen)
{
std::cout << i.Name << std::endl;
}
}

和我的测试.hpp

#include <vector>
#include <string>
#ifndef LABOR1TEST_TEST_HPP
#define LABOR1TEST_TEST_HPP

using PersonType = struct {
std::string Name;
};
void test(std::vector<PersonType> const &_Personen);
#endif //LABOR1TEST_TEST_HPP

CMakeLists.txt

cmake_minimum_required(VERSION 3.17)
project(LABOR1TEST)

set(CMAKE_C++_STANDARD 11)

add_executable(LABOR1TEST main.cpp test.cpp test.hpp)

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