由于某种原因,Visual Studio突然拒绝构建我的c ++文件。另外,当它弹出“出现构建错误,您想继续上一次成功的构建”时,我按是,然后说无法启动程序。我正在运行具有最新更新的Windows 10,并且还具有最新更新的Visual Studio 2019。
当它说无法启动程序时会发生什么:
这是我的构建输出显示的内容: 编辑:这是我删除所有变量副本并梳理所有先前错误之后的新版本:
1>------ Build started: Project: MasterApp, Configuration: Debug Win32 ------
1>MasterApp.cpp
1>C:\Users\foo\source\repos\MasterApp\MasterApp.cpp(33,15): warning C4244:
'argument': conversion from 'time_t' to 'unsigned int', possible loss of data
1>masterappRef.cpp
1>Generating Code...
1>masterappRef.obj : error LNK2005: "double version" (?version@@3NA) already
defined in MasterApp.obj
1>C:\Users\foo\source\repos\MasterApp\Debug\MasterApp.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Done building project "MasterApp.vcxproj" -- FAILED
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
下面的代码:
#pragma comment(lib, "winmm.lib")
// MasterApp.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <time.h>
#include "MasterAppRef.h"
#include <stdlib.h>
using namespace std;
//global variables
string passwd_admin = "admin";
string PassEntered;
string Username1 = "admin";
string Username2 = "administrator";
string UserEntered;
double version = 2.4;
int main()
{
//setting console window title
SetConsoleTitle ( TEXT("Master App"));
//seed for the randomiser
srand(time(NULL));
//declaring variable
int randomDice;
system("cls");
cout << "Please login\n";
cout << "\n";
cout << "Enter Username: ";
cin >> UserEntered;
if (UserEntered == Username1) {
}
else if (UserEntered == Username2) {
}
else if (UserEntered != Username2 && UserEntered != Username1) {
return 0;
}
else {
return 0;
}
system("cls");
cout << "Please login\n\n";
cout << "Enter Password: ";
cin >> PassEntered;
if (PassEntered == passwd_admin) {
cout << "\nAccess Granted!\n\n\n";
}
else {
cout << "Wrong Password";
return 0;
}
system("cls");
cout << "\n\n [ Starting ]\n";
cout << " Press enter to continue";
system("pause >nul");
system("cls");
Sleep(2200);
cout << "\n\n\nMaster App " << version << " 2020\n\n";
Sleep(1530);
bool played = PlaySound(TEXT("C:\\foo\\startup.wav"), NULL, SND_ASYNC);
Sleep(3000);
int choice;
system("cls");
cout << "\nMaster App Version " << version << "\n\n";
cout << "Choose and option and then press enter\n";
cout << "(E.g. 1 [PRESS ENTER] will give you choice number 1)\n\n";
cout << "1) Close App\n";
cout << "2) Roll the Dice\n";
cout << "3) Pull up some ASCII art\n";
cout << "4) Give me a random number\n";
cout << "5) Give me some insparation\n";
cout << "6) Open Administrator Panel\n";
cout << "7) Play music\n";
cout << "8) Open Command Prompt\n";
cout << "9) See Change Log\n";
cout << "10) Open Calculator\n";
cout << "11) Open New Notepad\n";
cout << "12) Clock\n";
cout << "13) Ask the magical 8 ball\n";
cout << "\n\nYour choice: ";
cin >> choice;
if (choice == 1) {
return 0;
}
else if (choice == 2) {
randomDice = rand() % 6 + 1;
cout << "\n "<<"And your dice has rolled... " << randomDice <<"!\n";
system("pause");
}
else if (choice == 3) {
//This is the only ascii art that doesn't have backslashes.
cout << " _______ _______ _______ ___ ___ _______ ______ _______ __ \n";
cout << "| _ || || || | | | | _ || _ | | || |\n";
cout << "| |_| || _____|| || | | | | |_| || | || |_ _|| | \n";
cout << "| || |_____ | || | | | | || |_||_ | | | |\n";
cout << "| ||_____ || _|| | | | | || __ | | | |__| \n";
cout << "| _ | _____| || |_ | | | | | _ || | | | | | __ \n";
cout << "|__| |__||_______||_______||___| |___| |__| |__||___| |_| |___| |__| \n";
system("pause");
}
else if (choice == 4) {
int randomNumber;
randomNumber = rand() % 10000 + 1;
cout << "\n " << "Your random number is: " << randomNumber << "\n";
system("pause");
}
else if (choice == 5) {
int inspNum;
inspNum = rand() % 6 + 1;
if (inspNum == 6) {
cout << "Life is like riding a bicycle. To keep your balance, you must keep moving.\n";
system("pause");
}
else if (inspNum == 5) {
cout << "Those who think they are dumb are actually the smart ones...\n";
system("pause");
}
else if (inspNum == 4) {
cout << "Act like what you're doing matters - It does.\n";
system("pause");
}
else if (inspNum == 3) {
//By Albert Einstein
cout << "Success is not final, failure is not fatal: it is the courage to continue that counts.\n";
system("pause");
}
else if (inspNum == 2) {
cout << "Never bend your head. Always hold it high. Look the world straight in the eye.\n";
system("pause");
}
else if (inspNum == 1) {
cout << "What you get by achieving your goals is not as important as what you become by achieving your goals.\n";
system("pause");
}
else {
cout << "Error!\n";
system("pause");
return 0;
}
}else if (choice == 6) {
system("cls");
string adminName = "Administrator";
string adminPass = "administrator";
string iu;
string ip;
cout << "Enter Administrator Username: ";
cin >> iu;
if (iu == adminName) {
cout << "Enter Password: ";
cin >> ip;
if (ip == adminPass) {
cout << "Access Granted! You may proceed now.\n\n";
system("pause");
}
else {
cout << "Incorrect Password!\n";
system("pause");
return 0;
}
}
else {
cout << "Incorect Username!\n";
system("pause");
return 0;
}
system("cls");
cout << "Administrator Panel\n\n";
int adminChoice;
cout << "Options:\n";
cout << "1) See Change Log\n";
cout << "2) Close App\n";
cout << "3) Change Colour\n";
cin >> adminChoice;
if (adminChoice == 1) {
system("cls");
cout << "Change Log\n\n";
cout << "Version " << version << "\n\n";
cout << "1) More options\n";
cout << "2) Added more music and credited music artists/websites\n";
cout << "3) Administrator panel discontinued - slowly migrating to main panel\n";
cout << "4) Added something new...\n";
}
else if (adminChoice == 2) {
system("cls");
cout << "Closing...";
Sleep(3000);
return 0;
}
else if (adminChoice == 3) {
cout << "Switch Colour to...\n\n";
cout << "1) Default\n";
cout << "2) High Contrast\n";
int colourChoice = 1;
cin >> colourChoice;
if (colourChoice == 1) {
system("color");
system("cls");
system("pause");
}
else if (colourChoice == 2) {
system("color fc");
system("pause <nul");
}
}
}
else if (choice == 7) {
system("cls");
cout << "Choose a song to play:\n\n";
cout << "Sample Music\n\n";
cout << "1) Beethoven's 9th Symphony (11:48)\n";
cout << "2) Beethoven's 1st Symphony (26:12)\n";
cout << "\nUpbeat Music\n\n";
cout << "3) Piano Piece (2:02)\n";
cout << "\nMore coming soon!\n";
int song;
cin >> song;
if (song == 1) {
system("cls");
cout << "\nPLaying: Beethoven's 9th Symphony\n\n";
PlaySound(TEXT("C:\\foo\\beethoven.wav"), NULL, SND_ASYNC);
cout << "\n\n\nPress any key to stop playing\n";
system("pause >nul");
}
else if (song == 2) {
system("cls");
cout << "\nPlaying: Beethoven's 1st Symphony\n\n";
PlaySound(TEXT("C:\\foo\\beethoven1.wav"), NULL, SND_ASYNC);
cout << "\n\n\nPress any key to stop playing\n";
system("pause >nul");
}
else if (song == 3) {
system("cls");
cout << "\nPlaying: Upbeat Piano Piece\n\n";
PlaySound(TEXT("C:\\foo\\upbeatpiano.wav"), NULL, SND_ASYNC);
cout << "\n\n\nPress any key to stop playing\n";
system("pause >nul");
}
}
else if (choice == 8) {
system("cmd.exe");
system("cd C:\\Windows\\System32");
system("pause >nul");
}
else if (choice == 9) {
void versionInfo();
}
else if (choice == 10) {
system("cls");
system("calc.exe");
cout << "Opening Calculator...\n";
system("pause >nul");
}
else if (choice == 11) {
system("notepad");
}
else if (choice == 12) {
system("cls");
system("date /t");
cout << "\n";
system("time /t");
cout << "\n";
cout << "\n";
system("pause");
}
else if (choice == 13) {
system("cls");
int eightBall;
eightBall = rand() % 6 + 1;
cout << "Ask the magical eight-ball a question! Press enter when you have it...";
system("pause >nul");
system("cls");
if (eightBall == 1) {
cout << "Think harder and try again...\n";
system("pause");
void eightBallFunction();
}
else if (eightBall == 2) {
cout << "Seems like a good idea\n";
system("pause");
void eightBallFunction();
}
else if (eightBall == 3) {
cout << "No, don't.\n";
system("pause");
void eightBallFunction();
}
else if (eightBall == 4) {
cout << "YAAAAASSS!\n";
system("pause");
void eightBallFunction();
}
else if (eightBall == 5) {
cout << "Hmm... I didn't get that. Try again.\n";
system("pause");
void eightBallFunction();
}
else if (eightBall == 6) {
cout << "Um, sure\n";
system("pause");
void eightBallFunction();
}
else if (eightBall == 7) {
cout << "It's up to you, but I would say no.\n";
system("pause");
void eightBallFunction();
}
else if (eightBall == 8) {
cout << "Well, I would think that you shouldn't\n";
system("pause");
void eightBallFunction();
}
}
else if (choice == 14) {
system("cls");
}
}
MasterAppRef.h:
#pragma once
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main();
//declaring functions
void versionInfo();
void eightBallFunction();
MasterAppRef.cpp:
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <time.h>
#include "MasterAppRef.h"
#include <stdlib.h>
using namespace std;
double version;
}
void eightBallFunction() {
system("cls");
int TryeightBall;
TryeightBall = rand() % 6 + 1;
cout << "Ask the magical eight-ball a question! Press enter when you have it...";
system("pause >nul");
system("cls");
if (TryeightBall == 1) {
cout << "Think harder and try again...\n";
system("pause");
void eightBallFunction();
}
else if (TryeightBall == 2) {
cout << "Seems like a good idea\n";
system("pause");
void eightBallFunction();
}
else if (TryeightBall == 3) {
cout << "No, don't.\n";
system("pause");
void eightBallFunction();
}
else if (TryeightBall == 4) {
cout << "YAAAAASSS!\n";
system("pause");
void eightBallFunction();
}
else if (TryeightBall == 5) {
cout << "Hmm... I didn't get that. Try again.\n";
system("pause");
void eightBallFunction();
}
else if (TryeightBall == 6) {
cout << "Um, sure\n";
system("pause");
void eightBallFunction();
}
else if (TryeightBall == 7) {
cout << "It's up to you, but I would say no.\n";
system("pause");
void eightBallFunction();
}
}
任何帮助将不胜感激!
答案 0 :(得分:1)
在您提供的第一段代码中,您已经
double version = 2.4;
在MasterAppRef.cpp中,您已经拥有
double version;
因此,您有version
的两个定义。如果这些应该引用相同的变量,则应将其放在单个头文件中:
extern double version;
然后,两个.cpp文件都将包含该头文件。然后,只有一个.cpp文件具有定义(例如,您的第一个块)。如果应该引用不同的变量,则有两个选择。一,将它们重命名为唯一的名称。或两个,将它们放在匿名名称空间中。即
namespace {
double version;
}
答案 1 :(得分:-1)
如果代码没有问题,则您的程序在关闭后可能会运行,因此VS无法访问您的程序文件,请使用cmd命令任务列表搜索程序,如果存在,请使用taskkill将其杀死,或者只是重新加载您的PC。