2012-08-15 99 views
0

解決我的「WorkoutGeneratorMain.cpp」被IDE分類爲C++頭。我不知道爲什麼發生,但我修好了。現在我可以處理我所有的其他錯誤。VS2010鏈接器錯誤

謝謝大家!

============================================== =====

在Visual Studio 2010專業版編譯時,我的計劃,我得到以下錯誤:

------構建開始:項目:WorkoutGenerator,配置:調試的Win32 --- ---
開始建造2012/8/15 12:19:18 PM。
InitializeBuildStatus:
   觸摸「Debug \ WorkoutGenerator.unsuccessfulbuild」。
ClCompile:
    LiftClass.cpp
ManifestResourceCompile:
   所有輸出均達到最新。
MSVCRTD.LIB(crtexe.obj):錯誤LNK2019:解析外部符號主要在結引用_ _tmainCRTStartup
C:\用戶\ Shanalex \文件\編程\ C++編程\ WorkoutGenerator \ WorkoutGenerator \調試\ WorkoutGenerator .exe:致命錯誤LNK1120:1個未解析的外部設備

在我的搜索中,我發現了幾個指南來解決這個問題;但是,他們幾乎都提示該文件是設置爲控制檯設置的Windows應用程序,反之亦然。我的程序是一個控制檯應用程序,所有設置對於win32控制檯應用程序來說都是正確的。一些地方有鏈接錯誤,但我似乎沒有任何其他人有我的項目設置的問題。

我對C++和VS2010中的多部分程序相當陌生。我可以很容易地犯一個基本的錯誤,但是在將我的代碼與各種教程和書籍的代碼進行比較時,我一直無法找到它。

我有三個代碼文件,如下所示:

LiftClass.h

//Lift Classes 
//Defines the Lift Class 


#ifndef LIFTCLASSHEADER_H_INCLUDED 
#define LIFTCLASSHEADER_H_INCLUDED 

#include <iostream> 
#include <string> 
#include <vector> 
#include <random> 
#include <ctime> 

using namespace std; 

class Lift 
{ 
public: 
    string LName; 
    string LType; 
    string LBody; 
    vector<double> LLoadScale; 

    Lift(string Name, string Type, string Body, 
     double Pawn, double Bishop, double Knight, double Rook, double Royal); 
}; 

Lift::Lift(string Name, string Type, string Body, 
    double Pawn, double Bishop, double Knight, double Rook, double Royal) 
{ 
    LName = Name, 

    LType = Type, 

    LBody = Body, 

    LLoadScale.push_back(Pawn), 
    LLoadScale.push_back(Bishop), 
    LLoadScale.push_back(Knight), 
    LLoadScale.push_back(Rook), 
    LLoadScale.push_back(Royal); 
} 


#endif 

然後,我有我的電梯類的.cpp實現,併爲他們隨機化的功能。

LiftClass.cpp

//Exercise Randomizer using Lift Class 
//Initializes Lifts for use in Workout Generator 
//Version 2.0 will reference Database 

#include "LiftClass.h" 

Lift exerciseRandomizer() //Define database of exercise & randomly select one 
{  
    vector<Lift> LiftDatabase; 

    Lift Clean("Clean", "Olympic", "Full", .33, .66, 1, 1.33, 1.66); 
    Lift Bench("Bench Press", "Heavy", "Upper", .33, .66, 1, 1.5, 2); 

    LiftDatabase.push_back(Clean); 
    LiftDatabase.push_back(Bench); 

    srand(static_cast<unsigned int>(time(0))); //Seed random number 

    unsigned randomNumber = rand(); //Generate Random Number 

    //Get random between 1 and total lift count 
    unsigned randomSelector = (randomNumber % LiftDatabase.size()); 

    return LiftDatabase[randomSelector]; 
} 

最後,我有我的主要功能WorkoutGeneratorMain.cpp

WorkoutGeneratorMain.cpp

//Workout Generator 
//Generates workouts based on goal and fitness level 

#include "LiftClass.h" 


int main() 
{ 
    exerciseRandomizer(); 

    Lift LiftA = exerciseRandomizer(); 

    cout << "\n\nYour first lift is: " << LiftA.LName << "\n\n Its lift type is: " << LiftA.LType << endl; 
    cout << "\n\nGood Luck!" << endl; 

    system("pause"); 
    return 0; 
} 

任何建議都非常讚賞。

感謝,

-Alex

+1

題外話題,但你應該避免使用名稱空間標準;特別是在頭文件中。 – juanchopanza 2012-08-15 18:21:59

+3

我看到'LiftClass.cpp'被編譯:'ClCompile:LiftClass.cpp'。但是我沒有看到'WorkoutGeneratorMain.cpp'的相同消息。你確定,它包含在你的項目中嗎? – Lol4t0 2012-08-15 18:25:46

+0

如果您正在鏈接不使用相同運行時的組件,則可能會出現此錯誤。像單線程/多線程,調試/發佈等。 – 2012-08-15 18:27:31

回答

3

你會認爲int main()是可執行文件的入口點,但它不是(必然)。 :)根據項目設置,運行時可能會調用wmainmain。這就是爲什麼你使用_tmain,這是一個擴展到運行時所期望的宏。

嘗試將其更改爲:

int _tmain(int argc, _TCHAR* argv[]) 

PS - 這應該是自動生成的,也許你刪除它,而不是替代的_tmain內容。

+1

編譯器應根據您選擇的運行時間爲您處理。如果不是這樣,那麼該項目可能沒有正確的運行時間來連接它所連接的庫...... – 2012-08-15 18:28:38

+0

'int main()'不應該引起任何問題。 '_tmain'允許Unicode應用程序以Unicode字符串的形式接收命令行參數,但不要求_tmain'使用'_tmain'。 – 2012-08-15 18:32:40

+0

@PeterRitchie我認爲你的意思是IDE ... – 2012-08-15 18:49:32