C++:" multiple definition of 'mainCRTStartup' "错误等



所以我正在做一些初学者挑战,并想修改我的代码,这是我首先做的。

#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_disregister_frame'的多重定义
  • 对"_Jv_RegisterClasses"的未定义引用|

我猜您没有将旧文件从项目中排除。在这种情况下,链接器遇到两个main函数,不知道该使用什么。可能的解决方法:

  1. 从项目中排除未使用的文件
  2. 注释掉旧版本
  3. 使用条件编译:

    #ifdef OLD_VER
    // main1
    ...
    #else
    // main2
    ...
    #endif
    
  4. 创建一个新项目
  5. 使用版本控制系统

前三种方法不建议长期使用。最后一点很好(我认为是最好的一点),但它可能需要初级VCS学习。

最新更新