[英]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中刪除了舊代碼,然后編寫了它,但是當我嘗試運行它時,出現了以下錯誤消息:
猜猜您沒有從項目中排除舊文件。 在這種情況下,鏈接器滿足兩個main
功能,並且不知道使用什么。 可能的解決方法:
使用條件編譯:
#ifdef OLD_VER // main1 ... #else // main2 ... #endif
不建議長期使用前3種方法。 最后一個是一個很好的觀點(我認為是最好的觀點),但這可能需要基本的VCS學習。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.