C++ 如何修复对象文件中的重复函数?
我正在构建一个程序,只是为了提高我的OOP技能。这是我的项目结构:C++ 如何修复对象文件中的重复函数?,c++,oop,c++11,g++,project-structure,C++,Oop,C++11,G++,Project Structure,我正在构建一个程序,只是为了提高我的OOP技能。这是我的项目结构: Project -.vscode -build -includes - ContactInfo.h - Customer.h - Owner.h - PersonalInfo.h - User.h -src - ContactInfo.cpp - Customer.cpp - Owner.cpp - PersonalInfo.cpp - User.cpp
Project
-.vscode
-build
-includes
- ContactInfo.h
- Customer.h
- Owner.h
- PersonalInfo.h
- User.h
-src
- ContactInfo.cpp
- Customer.cpp
- Owner.cpp
- PersonalInfo.cpp
- User.cpp
- wrapper.h
- wrapper.cpp
- main.cpp
- CMakeLists.txt
问题是,当我在构建文件路径的cmd中键入commandmake
时,会出现以下错误:
duplicate symbol '_key' in:
CMakeFiles/make_test_project.dir/src/main.cpp.o
CMakeFiles/make_test_project.dir/src/wrapper.cpp.o
duplicate symbol '_customers' in:
CMakeFiles/make_test_project.dir/src/main.cpp.o
CMakeFiles/make_test_project.dir/src/wrapper.cpp.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [make_test_project] Error 1
make[1]: *** [CMakeFiles/make_test_project.dir/all] Error 2
make: *** [all] Error 2
我可以猜到这里发生了什么,我基本上在main.cpp
和wrapper.cpp
文件中都包含了wrapper.h
文件,但我不知道如何修复它。我也想保持我的项目结构这样。那么有没有办法修复这个错误呢
这是我的wrapper.h
文件:
#ifndef WRAPPER_H
#define WRAPPER_H
#include "../includes/Customer.h"
#include <vector>
#include <iostream>
char key;
void printUI();
void RunApplication();
PersonalInfo CreatePersonalInfo(std::string &, std::string &, int & );
ContactInfo CreateContactInfo(std::string &, std::string &, std::string &, int&);
void DisplayAllCustomers();
void AddCustomerToList(Customer &);
Customer CreateCustomer(PersonalInfo &, ContactInfo &);
std::vector<Customer> customers;
#endif
#include "wrapper.h"
int main()
{
RunApplication();
}
cmake_minimum_required(VERSION 3.13)
project(cmake_test_project)
set(CMAKE_CXX_STANDARD 17)
add_executable(make_test_project src/main.cpp src/wrapper.cpp src/ContactInfo.cpp src/PersonalInfo.cpp src/Customer.cpp src/User.cpp)
下面是我的CMakeLists.txt
文件:
#ifndef WRAPPER_H
#define WRAPPER_H
#include "../includes/Customer.h"
#include <vector>
#include <iostream>
char key;
void printUI();
void RunApplication();
PersonalInfo CreatePersonalInfo(std::string &, std::string &, int & );
ContactInfo CreateContactInfo(std::string &, std::string &, std::string &, int&);
void DisplayAllCustomers();
void AddCustomerToList(Customer &);
Customer CreateCustomer(PersonalInfo &, ContactInfo &);
std::vector<Customer> customers;
#endif
#include "wrapper.h"
int main()
{
RunApplication();
}
cmake_minimum_required(VERSION 3.13)
project(cmake_test_project)
set(CMAKE_CXX_STANDARD 17)
add_executable(make_test_project src/main.cpp src/wrapper.cpp src/ContactInfo.cpp src/PersonalInfo.cpp src/Customer.cpp src/User.cpp)
注意:我不打算共享wrapper.cpp
文件,因为它是一个几乎由120行组成的文件,但我可以向您提供我在wrapper.cpp
文件中包含的wrapper.h
文件的信息
请随时询问更多信息,谢谢。在您的包装中。h
extern char key;
void printUI();
void RunApplication();
PersonalInfo CreatePersonalInfo(std::string &, std::string &, int & );
ContactInfo CreateContactInfo(std::string &, std::string &, std::string &, int&);
void DisplayAllCustomers();
void AddCustomerToList(Customer &);
Customer CreateCustomer(PersonalInfo &, ContactInfo &);
extern std::vector<Customer> customers;
extern字符键;
void printUI();
void RunApplication();
PersonalInfo创建PersonalInfo(std::string&,std::string&,int&);
ContactInfo创建ContactInfo(std::string&,std::string&,std::string&,int&);
void DisplayAllCustomers();
无效添加客户列表(客户和);
客户创建客户(个人信息和联系人信息和);
外部标准::向量客户;
并在一个且仅在一个.cpp文件中声明
如果你有C++17,你可以用
inline
来交换extern,然后你就完成了。请参阅以解决至少一个问题,但是错误还提到了未定义的引用,这意味着链接器在任何源文件中都看不到printUI
的定义。更改为extern char键
并添加一个字符键
到一个.cpp文件。@然后查看链接的问题。您不能将全局变量的定义放在头文件中,除非它是内联的
(从C++17开始就可行)。您应该将这些变量移动到.cpp文件中,并且只在标题中移动extern
它们,以便在其他.cpp文件中访问它们。对客户
执行相同操作。第一件事是不要使用全局对象。如果你真的需要他们,那只是你的习惯问题,外人在哪里。谢谢!我对客户做了同样的事情
,一切都已修复。