簡體   English   中英

C ++:“'mainCRTStartup'的多個定義”錯誤等

[英]C++: “ multiple definition of 'mainCRTStartup' ” error etc

因此,我遇到了一些新手挑戰,想修改我的代碼,這是我首先要做的。

#include <iostream>
#include <stdlib.h>
#include <time.h>

int random;
int guess;
int num_guess = 1;

int main(){
    srand(time(NULL));
    random = rand() % 100 + 1;

    std::cout << "Try to guess my number between 1 and 100." << std::endl;
    std::cin >> guess;

    while(guess > random){
        std::cout << "Sorry too high but i'll give you another try." <<     std::endl;
        std::cin >> guess;
        num_guess += 1;
    }

    while(guess < random){
        std::cout << "Sorry too low but i'll give you another try." << std::endl;
        std::cin >> guess;
        num_guess += 1;
    }

    if(guess = random){
        std::cout << "WOW! Congratulations you actually got it, you did use " << num_guess << " tries tho." << std::endl;
    }


    return(0);
}

它應該生成一個1到100之間的隨機數,然后您猜它是多少。 但是后來我將此代碼復制到同一項目下的另一個文件中,因為我在學校里這樣做,所以我希望所有不同版本的代碼用於記錄目的。 但是,當我開始編寫新代碼時,該程序應該猜測一個數字,您可以輸入1到100之間的數字。

#include <iostream>
#include <stdlib.h>
#include <time.h>

int number;
int guess = 100;
int num_guess = 1;

int main(){
    std::cout << "Please enter any number between 1 and 100" << std::endl;
    std::cin >> number;
    if(number > 100 && number < 1){
        std::cout << "Enter a valid number" << std::endl;
        std::cin >> number;
    }

    srand(time(NULL));
    guess = rand() % guess + 1;


    return(0);
}

我從main.cpp中刪除了舊代碼,然后編寫了它,但是當我嘗試運行它時,出現了以下錯誤消息:

  • mainCRTStartup的多個定義
  • WinMainCRTStartup的多個定義
  • “ atexit”的多重定義
  • _onexit的多重定義
  • __gcc_register_frame的多重定義
  • __gcc_deregister_frame的多重定義
  • 對“ _Jv_RegisterClasses” |的未定義引用

猜猜您沒有從項目中排除舊文件。 在這種情況下,鏈接器滿足兩個main功能,並且不知道使用什么。 可能的解決方法:

  1. 從項目中排除未使用的文件;
  2. 注釋掉舊版本;
  3. 使用條件編譯:

      #ifdef OLD_VER // main1 ... #else // main2 ... #endif 
  4. 創建一個新項目;
  5. 使用版本控制系統。

不建議長期使用前3種方法。 最后一個是一個很好的觀點(我認為是最好的觀點),但這可能需要基本的VCS學習。

暫無
暫無

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

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