如何在一个cpp文件中引用另外一个cpp文件中定义的类

Ivan_You 2007-05-15 08:32:30
写了一个工程,包含两个cpp文件A.cpp和B.cpp,两个头文件A.h和B.h。A.cpp中定义了一个类A,B中要引用到此类,但是生成时总是有“error LNK2019: 无法解析的外部符号”这样的错误,提示找不到类A的实现,搞了很久没解决。
在B中include的了A.h, 并且A.cpp、B.cpp、A.h和B.h都定义在同一个工程中。尝试了在工具->选项->VC++目录的中包含了A.obj所在的路径,并且在项目属性->链接器->输入->附加依赖项中添加了A.obj,但是错误还是一样。
大概的代码是这样的
//A.h
class A{
...
void p();
...
};

//A.cpp
#include"A.h"
void A::p(){
...
}

//B.cpp
#include"A.h"
int main{
A a;
a.p();
return 0;
}

这些文件都在一个工程中,但是一生成就有"error LNK2019: 无法解析的外部符号"的错误,提示找不到函数p()的实现.
如果把#include"A.h"改成#include"A.cpp",就没有这个问题了,但是#include"A.cpp"这个写法太少见了,不知道这个问题正确的解决方法应该是怎样的
...全文
1219 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ivan_You 2007-05-21
  • 打赏
  • 举报
回复
非常感谢lightnut
已给分结帖
lightnut 2007-05-21
  • 打赏
  • 举报
回复
把你的LinearList.cpp的内容移到LinearList.h中;
chain.cpp(如果有)移到chain.h中.


模板的定义和实现要放在同一个头文件中!

lightnut 2007-05-21
  • 打赏
  • 举报
回复
模板的定义和实现要放在同一个头文件中!
Ivan_You 2007-05-21
  • 打赏
  • 举报
回复
不好意思
大概是这样的:
//LinearList.h
template<class T>
class LinearList{
friend ostream& operator<< <T>(ostream& out, LinearList<T>& x);
public:
LinearList()
......//其它成员函数的声明
LinearList<T>& Delete(int k, T& x);

private:
int length;
int MaxSize;
T* element;
int current;
};

//Linearlist.cpp
#include"LinearList.h"

template<class T>
LinearList<T>::LinearList()
{
MaxSize=1;
element=new T[MaxSize];
length=0;
current=0;
}

....//其它成员函数的定义

template<class T>
LinearList<T>& LinearList<T>::Insert(int k, const T& x)
{......
}

//Chain.h
#include"LinearList.h"
template<class T>
class ChainNode;

template<class T>
class Chain{
public:
Chain(){first=last=NULL;}
~Chain();

void FromList(LinearList<T>& );
void ToList(LinearList<T>& );
bool Reverse();
void Alternate(Chain<T>&, Chain<T>& );
void Sort();
void Merge(Chain<T>& , Chain<T>& );
void Split(Chain<T>& , Chain<T>& );
private:
ChainNode<T>* first;
ChainNode<T>* last;
};

template<class T>
class ChainNode{
friend class Chain<T>;
private:
T data;
ChainNode<T>* link;
};


Jofee 2007-05-18
  • 打赏
  • 举报
回复
楼主在类A中声明p()函数的时候,有没有加public?
C++,默认是私有的。
oyd 2007-05-18
  • 打赏
  • 举报
回复
楼主,你把错误信息连同其上下文照原样贴一下,不要自己加工
isarc 2007-05-18
  • 打赏
  • 举报
回复
main函数为什么放在b.cpp里面?
应该是a.h定义a的类a.cpp实现;b.h定义b的类b.cpp定义实现。如果b的cpp实现需要a则在b.h中加入include a.h
然后在另一个文件中
include a.h
include b.h
main()
、。。。。。
Ivan_You 2007-05-16
  • 打赏
  • 举报
回复
现在问题就是include头文件(含声明)不行,include cpp文件反而可以.
winner8080 2007-05-16
  • 打赏
  • 举报
回复
你#include "a.h"都不行吗
就是把有类声明的那个文件(h文件或cpp文件)include进去
是类声明的文件,不是类实现的文件
Ivan_You 2007-05-15
  • 打赏
  • 举报
回复
楼上两位的方法都试了,可是还是老问题.这个问题搞了好久,还是没解决,郁闷
chenyu2202863 2007-05-15
  • 打赏
  • 举报
回复
前向声明试试~
B.h
--------------
class A;
//....
chaorenfei 2007-05-15
  • 打赏
  • 举报
回复
//A.h
class A
{
...
public:
void p();
...
};

//A.cpp
#include "A.h"
void A::p()
{
...
}
//B.h
class A;
class B
{
...
};
//B.cpp
#include "A.h"
#include "B.h"
int main
{
A a;
a.p();
return 0;
}
Ivan_You 2007-05-15
  • 打赏
  • 举报
回复
这样肯定是可以的,只是是否规范的问题,include".h"和include".cpp"的作用都只是复制文档
Lancer_EVO 2007-05-15
  • 打赏
  • 举报
回复
#include "A.cpp"

这样可以吗?
星羽 2007-05-15
  • 打赏
  • 举报
回复
#include"A.cpp"

这个太强了

应该不会有这种问题的,不知道你是怎么搞的
Ivan_You 2007-05-15
  • 打赏
  • 举报
回复
上面的代码中我是这样做的啊,可是还是有"error LNK2019: 无法解析的外部符号"的错误
  • 打赏
  • 举报
回复
#include "a.h"
通常做法是
.h文件中写上class的声明,
.cpp文件写上实现.

33,320

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧