C++ C++;程序无法使用CMD正确编译,但是VSC中的CMD兼容
im编写的程序在IDE visual studio代码上成功运行。但是,当尝试使用Bitvise SSH客户端运行我的程序时,我会得到一个错误列表,我自己无法理解问题的原因。Bitvise它是从远程服务器访问CMD客户端的另一种方式,出于所有目的,它的行为与windows CMD相同。我将提供错误的屏幕上限,以及我认为导致错误的程序部分的完整运行情况。如果需要更多代码,请随时询问 错误屏幕盖 此错误报告显示了一个常见错误,其中某些内容是所有实例的占位符C++ C++;程序无法使用CMD正确编译,但是VSC中的CMD兼容,c++,ssh,compilation,header-files,C++,Ssh,Compilation,Header Files,im编写的程序在IDE visual studio代码上成功运行。但是,当尝试使用Bitvise SSH客户端运行我的程序时,我会得到一个错误列表,我自己无法理解问题的原因。Bitvise它是从远程服务器访问CMD客户端的另一种方式,出于所有目的,它的行为与windows CMD相同。我将提供错误的屏幕上限,以及我认为导致错误的程序部分的完整运行情况。如果需要更多代码,请随时询问 错误屏幕盖 此错误报告显示了一个常见错误,其中某些内容是所有实例的占位符 multiple definition
multiple definition of `something' /tmp/ccBhjFYn.o:(.bss+0x0): first defined here
此错误不会在visual studio代码中发生
从这个错误报告中可以看出,问题出现在driver.cpp和my header.h文件中。出于这个原因,我不会为这些文件提供最低限度的代码,但它们足够小,不需要
主要
头文件
#include <vector>
#include <string>
struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
} myCustomer;
struct Part {
char partCode;
std::string partName;
int maximum;
int minimum;
int complexity;
} myPart;
struct Builder {
std::string builderName;
int ability;
int variability;
} myBuilder;
bool fexists(const std::string filename);
std::vector<Part> readpartFile();
std::vector<Customer> readcustomerFile();
std::vector<Builder> readbuilderFile();
float complexity(const Customer& c, const std::vector<Part>& parts);
void robotComplexity(const std::vector<Part>& vecB, const std::vector<Customer>& vecC);
double variability(const std::vector<Customer>& customerList, const std::vector<Builder>& builderList);
std::vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);
void writeFile(std::vector<double> build);
#包括
#包括
结构客户{
std::字符串客户名称;
std::字符串projectName;
std::字符串列表部分;
}我的客户;
结构件{
字符部分编码;
std::字符串部分名;
int最大值;
最小整数;
智力复杂性;
}我的部分;
结构生成器{
std::字符串生成器名称;
智力;
int变异性;
}myBuilder;
bool-fexists(const-std::string文件名);
std::vector readpartFile();
std::vector readcustomerFile();
std::vector readbuilderFile();
浮点复杂性(常量客户和c、常量标准::向量和部件);
void机器人复杂性(const std::vector&vecB,const std::vector&vecC);
双重可变性(const-std::vector和customerList,const-std::vector和builderList);
向量构建尝试(构建器b,双变量,双复杂度);
void writeFile(std::vector build);
谢谢你的帮助。这个问题可能很难理解和理解,但我确实尽了最大的努力。欢迎提出任何有助于改进此问题的建议,但请友好:)这在标题中。h
struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
} myCustomer;
是全局变量的定义myCustomer
。因此,它不属于头文件
将头文件更改为
struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
};
extern Customer myCustomer; // global variable declaration
然后在您的一个cpp文件(我建议implementation.cpp
)中添加以下内容
或者你可以完全去掉全局变量(最好的解决方案)
注意,在我上面的一些评论中,我说过在头文件中有全局变量声明。我的意思是在头文件中有全局变量定义。定义和声明之间的区别在这里至关重要。将声明放在头文件中是可以的,但将定义放在头文件中是错误的。很抱歉造成混淆。
但是它们足够小,不需要一个。
LOL,这样的信心,有没有错?头文件中有变量声明。如果该头文件包含在两个或多个位置,则会出现所述错误。也许这就是VS编译和SSH编译之间的区别。为什么要在命令行中包含header.h
?将输出作为文本而不是图片放入问题中。@DavidLing不,当然不是,我已经说过好几次了,问题是头文件中有全局变量定义。不要那样做!啊,所以你建议不要添加std::string customerName;std::字符串projectName;std::StringListofParts to my struct Declarations必须将其保留为struct Customer{}@戴维林:你怎么能从这个答案中得出这个结论呢?@戴维林,天哪,不,上面是怎么说的?我已经详细说明了你应该做什么样的改变。问题不在于结构,这很好。问题在于myCustomer
和所有其他全局变量。
struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
};
extern Customer myCustomer; // global variable declaration
Customer myCustomer; // global variable definition