error LNK2019: 無法解析的外部符號
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include "Coordinate.h"
using namespace std;
/*****************************************/
/*對象數(shù)組
*要求:
* ? ? 定義Coordinate類
* ? ? 數(shù)據(jù)成員分別是m_iX,m_iY;
* ? ? 分別通過棧和堆實例化長度為3的對象數(shù)組
* ? ? 給數(shù)組中元素賦值
* ? ? 遍歷兩個數(shù)組
******************************************/
int main(void)
{
Coordinate coor[3];
coor[0].m_iX = 3;
coor[0].m_iY = 5;
Coordinate *p = new Coordinate[3];
p->m_iX = 7;
p[0].m_iY = 9;
p++;//p+=1;p=p+1;
p->m_iX = 11;
p[0].m_iY = 13;
p[1].m_iX=15;
p++;
p->m_iY = 17;
for (int i = 0; i < 3;i++)
{
cout <<"coor_X-"<<coor[i].m_iX << endl;
cout<<"coor_Y="<< coor[i].m_iY << endl;
}
for (int j = 0; j < 3;j++)
{
cout <<"p_X="<< p->m_iX << endl;
cout <<"p_Y="<<p->m_iY << endl;
p--;
}
p++;
delete[]p;
system("pause");
? ? return 0;
}
運行提示:
error LNK2019: 無法解析的外部符號 "public: __thiscall Coordinate::Coordinate(void)" (??0Coordinate@@QAE@XZ),該符號在函數(shù) _main 中被引用
1>demo3-duixiangshuzu.obj : error LNK2019: 無法解析的外部符號 "public: __thiscall Coordinate::~Coordinate(void)" (??1Coordinate@@QAE@XZ),該符號在函數(shù) "public: void * __thiscall Coordinate::`vector deleting destructor'(unsigned int)" (??_ECoordinate@@QAEPAXI@Z) 中被引用
1>E:\C++練習(xí)\demo3-duixiangshuzu\Debug\demo3-duixiangshuzu.exe : fatal error LNK1120: 2 個無法解析的外部命令
1>
1>生成失敗。
2015-09-08
解決方案:頭文件和源文件都在同一個項目中(即Coordinate.h和Coordinate.cpp一起放在項目中),頭文件中定義方法,在源文件中實現(xiàn),并且方法前加上Coordinate::,關(guān)鍵是Coordinate.cpp中加上include “stdafx.h”
”stdafx.h“的原理:
編譯器通過一個頭文件stdafx.h來使用預(yù)編譯頭文件。stdafx.h這個頭文件名是可以在project的編譯設(shè)置里指定的。編譯器認(rèn)為,所有在指令#include "stdafx.h"前的代碼都是預(yù)編譯的,它跳過#include "stdafx. h"指令,使用projectname.pch編譯這條指令之后的所有代碼。
因此,所有的MFC實現(xiàn)文件第一條語句都是:#include "stdafx.h"。在它前面的所有代碼將被忽略,所以其他的頭文件應(yīng)該在這一行后面被包含。否則,你將會得到“No such file or directory”這樣讓你百思不得其解的錯誤提示。
當(dāng)我們使用AppWizard來自動生成某些項目的時候,系統(tǒng)會自動把所需要include的頭文件在stdafx.h中先include一下,這樣,我們只需要直接include這個stdafx.h文件即可.因為同一個項目中的不同源文件CPP都包含相同的include文件,這樣,為每個.CPP文件都重復(fù)include這些文件就顯得很傻了。當(dāng)然如果你不用MFC的話就不用了。即:在每個.cpp文件中都include stdafx.h 就相當(dāng)于包含了其他的如iostream.h等文件