簡體   English   中英

C++ 程序無法使用 CMD 正確編譯,但是符合 VSC

[英]C++ Program cannot compile properly using CMD howerever complies within VSC

我正在編寫的程序在 IDE Visual Studio 代碼上成功運行。 但是,當嘗試使用 Bitvise SSH 客戶端運行我的程序時,我得到一個我自己無法理解問題的錯誤列表。 Bitvise 它是從遠程服務器訪問 CMD 客戶端的另一種方式,出於所有密集目的,它的行為與 windows CMD 相同。 我將提供錯誤的屏幕截圖和我認為導致錯誤的程序部分的完整運行。 如果需要任何進一步的代碼,請隨時詢問。

錯誤屏幕帽

在此處輸入圖像描述

此錯誤報告顯示了一個常見錯誤,其中某事是所有實例的占位符。

multiple definition of `something' /tmp/ccBhjFYn.o:(.bss+0x0): first defined here

此錯誤不會發生在 Visual Studio 代碼中

從這個錯誤報告中可以看出這個問題是在 driver.cpp 和我的 header.h 文件中發現的。 出於這個原因,我不會為這些文件提供最少的代碼,但它們足夠小,不需要一個。

主要的

int main()
{
    Customer c;
    Part p;
    Builder b;
    const string fileName = "Parts.txt";

    auto partsVec =  readpartFile();
    auto customerVec = readcustomerFile();
    auto builderVec = readbuilderFile();

    fexists(fileName);
    complexity(c, partsVec);
    robotComplexity(partsVec,customerVec);
    writeFile(buildAttempt(b, complexity(c, partsVec), variability(customerVec, builderVec)));


  return 0;
}

HEADER 文件



#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);

感謝您的任何幫助。 這個問題可能很難理解和理解,但我確實盡力了。 歡迎提出任何有助於改善這個問題的建議,但請保持友好:)

這在 header.h

struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
} myCustomer;

是全局變量myCustomer的定義。 因此,它不屬於 header 文件。

將 header 文件更改為此

struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
};

extern Customer myCustomer; // global variable declaration

然后到你的一個 cpp 文件(我建議implementation.cpp )添加這個

Customer myCustomer; // global variable definition

或者你可以完全取消全局變量(最好的解決方案)。

注意在我上面的一些評論中,我說你的 header 文件中有全局變量聲明。 我的意思是你的 header 文件中有全局變量定義 定義和聲明之間的區別在這里至關重要。 可以將聲明放在 header 文件中,但放置定義是錯誤的。 很抱歉有任何混淆。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM