| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2544 人关注过本帖
标题:请教关于error LNK2005的问题
只看楼主 加入收藏
thunderxu
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-4-27
收藏
 问题点数:0 回复次数:9 
请教关于error LNK2005的问题
编程时遇到一个关于error LNK2005的问题,不知道那位高手能帮我解决!不胜感激!

我定义了两个类,geo和coe。两个类的成员数据和成员函数的申明分别写在两个文件里,"geometrical.h","coeffcient.h"和"geometrical.cpp"和"coeffcient.cpp"。
同时,class coe: private geo
{
……………………
}

在每个".cpp"文件的开头都#include 对应的".h",如下所示(coe.cpp有所不同)
//geometrical.cpp
#include "geometrical.h"
………………
………………

//coeffcient.cpp
#include "geometrical.cpp"
#include "coeffcient.h"
………………
………………

最后在main()里:
#include "coefficient.cpp"
编译通过了,但在link时发生了如下的错误:
geometrical.obj : error LNK2005: "public: __thiscall geo::geo(double,double,double,int,int,int)" (??0geo@@QAE@NNNHHH@Z) already defined in coefficient.obj
geometrical.obj : error LNK2005: "protected: void __thiscall geo::fndimension(void)" (?fndimension@geo@@IAEXXZ) already defined in coefficient.obj
main.obj : error LNK2005: "public: __thiscall geo::geo(double,double,double,int,int,int)" (??0geo@@QAE@NNNHHH@Z) already defined in coefficient.obj
main.obj : error LNK2005: "protected: void __thiscall geo::fndimension(void)" (?fndimension@geo@@IAEXXZ) already defined in coefficient.obj
main.obj : error LNK2005: "public: __thiscall coe::coe(double,double,double,int,int,int)" (??0coe@@QAE@NNNHHH@Z) already defined in coefficient.obj
main.obj : error LNK2005: "public: void __thiscall coe::fncoedefine(void)" (?fncoedefine@coe@@QAEXXZ) already defined in coefficient.obj
main.obj : error LNK2005: "public: void __thiscall coe::fncoefficientF(double * * *,double * * *,double * * *,double,double)" (?fncoefficientF@coe@@QAEXPAPAPAN00NN@Z) already defined in coefficient.obj
main.obj : error LNK2005: "public: void __thiscall coe::fnDUVW(void)" (?fnDUVW@coe@@QAEXXZ) already defined in coefficient.obj
main.obj : error LNK2005: "public: void __thiscall coe::fncoefficientP(double)" (?fncoefficientP@coe@@QAEXN@Z) already defined in coefficient.obj
main.obj : error LNK2005: "public: void __thiscall coe::fnpseudoV(double * * *,double * * *)" (?fnpseudoV@coe@@QAEXPAPAPAN0@Z) already defined in coefficient.obj
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/test1.exe : fatal error LNK1120: 1 unresolved externals

每个头文件里都写了语句#pragma once,但仍然出现这种错误。

我又在每个头文件里写了
#ifndef ***.h
#define ***.h

………………

#endif
但还是不行。

小弟为VC++菜鸟,还往那位高手不惜赐教!万分感谢!
搜索更多相关主题的帖子: cpp error geometrical coeffcient 
2006-05-02 14:45
琥珀之风
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2005-11-3
收藏
得分:0 

#include "geometrical.cpp"

#include "coefficient.cpp"

一般很少include"*.cpp"文件...
所以可能出现一些诡异的问题

改成".h"再式式

恶魔只是将人类引入堕落的深渊 天使吹响了末日审判的号角 ——恶魔的追随者
2006-05-02 18:49
thunderxu
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-4-27
收藏
得分:0 

多谢!

2006-05-03 08:52
thunderxu
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-4-27
收藏
得分:0 
但是如果不#include "*.cpp"文件,怎么才能由一个main文件调用其他的cpp文件呢?因为其他的cpp文件都是写在不同的文件中的呀!
2006-05-03 09:10
–★–
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1512
专家分:0
注 册:2006-5-1
收藏
得分:0 
回复:(thunderxu)但是如果不#include "*.cpp"文件,...
楼上的意见是说,“包含”进来的文件名为何非要叫它
*.cpp呢,他建议你用*.H作为扩展名再试一试。

落霞与孤鹜齐飞,秋水共长天一色! 心有多大,路有多宽。三教九流,鸡鸣狗盗。兼收并蓄,海纳百川。
2006-05-04 21:04
thunderxu
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-4-27
收藏
得分:0 
不行,用.h做扩展名还是不行
是不是因为我有一个类的继承呀!
2006-05-04 22:32
thunderxu
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-4-27
收藏
得分:0 
这是我的全部代码。可能有点长!

/******************************************************/
/******************************************************/
#include "geometrical.cpp"
#include "coefficient.h"
#include <cmath>
void coe::fncoedefine()
{
int i,j,k;
…………
}

2006-05-04 23:15
thunderxu
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-4-27
收藏
得分:0 
void coe::fncoefficientF(double ***FXI,double ***FYI,double ***FZI,double coeRO,double coeDif) //coefficients for variables, such as u,v,T, etc.
{
……………………
……………………
……………………
}
void coe::fnDUVW()
{
int i,j,k;
…………………………
}
//**********************************************************
void coe::fncoefficientP(double RO) //calculate the coefficients for pressure
{
…………………………
}
/*****************************************************/
/*****************************************************/
void coe::fnpseudoV(double ***F,double ***coeSOURCE)
{
……………………
}



#include <iostream.h>
#include "geometrical.h"

void geo::fndimension()
{

……………………
}



//coefficient.h
#pragma once
class coe:private geo
{
………………
};

//geometrical.h
#pragma once
class geo
{
……………………
};


//#include "geometrical.h"
//#include "geometrical.h"
//#include "coefficient.h"
#include "coefficient.h"
#include <iostream.h>
int main()
{
……………………
……………………
return 0;
}

//*****************************************************************
void fndefine()
{

…………
}
//******************************************************************

代码太长,我只能把主要的列出来!
还希望哪位高手能帮我解决这个问题!
2006-05-04 23:15
momo_2003
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2006-5-2
收藏
得分:0 

遇到这种问题,我觉得需要创建一个工程,比如用VS2005版的时候,要创建一个工程项目才能够运行,而对于一般的只有一个*.cpp的文件是不支持运行的,所以对于这种链接的时候出的问题,用一个工程是比较清晰明了的,把所有的声明和定义都放到一个名为*.h中(当然这需要建立一个.h格式类型的文件才行),然后直接在主程序中写上#include <*.h>,我想就可以了吧,你可以试试看~~~

2006-05-05 01:54
thunderxu
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-4-27
收藏
得分:0 
但是如果那样的话会占用大量的计算机内存呀!会影响计算速度!
2006-05-05 08:42
快速回复:请教关于error LNK2005的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015545 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved