.h,.cpp编译问题??急急急急急急

时间:2023-02-21 23:54:48
三个文件Cat.h,Cat.cpp,Main.cpp分别如下,当我编译Main.cpp时提示:
--------------------Configuration: 112 - Win32 Debug--------------------
Compiling...
Main.cpp
d:\myprojects\Cat.h(2) : error C2011: 'Cat' : 'class' type redefinition
Error executing cl.exe.
有人知道怎么回事吗?另外请教多文件到底应怎样编译?回答正确马上给分!

//Cat.h
class Cat
{
public:
int GetAge();
void SetAge(int age);
void Meow();
protected:
int itsAge;
};



//Cat.cpp
#include <iostream.h>
#include "Cat.h"

int Cat::GetAge()
{
return itsAge;
}

void Cat::SetAge(int age)
{
itsAge=age;
}

void Cat::Meow()
{
cout<<"Meow.\n";
}


//Main.cpp
#include <iostream.h>
#include "Cat.h"
#include "Cat.cpp"

void main()
{
Cat frisky;
frisky.SetAge(5);
frisky.Meow();
cout<<"frisky is a cat who is"
<<frisky.GetAge()
        <<"years old.\n";
frisky.Meow();
}

10 个解决方案

#1


//Main.cpp
#include <iostream.h>
#include "Cat.h"
#include "Cat.cpp"        // 此行应去掉

#2


多源程序文件,应分别编译,生成目标文件之后再使用link将它们所对应的目标文件链接。
如使用GNU C++为例:
g++ -c main.cpp
g++ -c cat.cpp
g++ -l main.o cat.o -o main
(大概是这样,具体记不清了)

#3


你用VC的话就通过Add Project >> Files...来添加相关的程序文件,不要自己新建之后就直接编译。

#4


1楼的正确

#5


如果你没有集成的编译工具,那么就要编写makefile了!具体的语法我不记得了!

#6


Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/112.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

112.exe - 2 error(s), 0 warning(s)

这种错误又指什么?我就这两个错误了@^@,大家加油!

#7


可能是你的某个程序文件找不到main、WinMain之类的函数而导致链接失败。

#8


你没有必要include cat.cpp文件,因为在cat.cpp中已经存在了cat.h,你main.cpp里面又
include了一次,所以重复,解决方法可以修改cat.h
#ifndef CAT_H
#define CAT_H
原来cat.h的内容
#endif
这样就没有问题了,但是你最好去掉#include "cat.cpp",你是用的vc,所以vc会自动帮你
link他们的,如果在linux下面,倒是需要手工分别将其编译成.o,然后再link

#9


楼上正解

#10


Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/112.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

112.exe - 2 error(s), 0 warning(s)

这种错误又指什么?我就这两个错误了@^@,大家加油!

看来你是用VC的win32 application向导写win32 console app了
你建一个空的win32 console app项目,把你的文件添加进去就行了

#1


//Main.cpp
#include <iostream.h>
#include "Cat.h"
#include "Cat.cpp"        // 此行应去掉

#2


多源程序文件,应分别编译,生成目标文件之后再使用link将它们所对应的目标文件链接。
如使用GNU C++为例:
g++ -c main.cpp
g++ -c cat.cpp
g++ -l main.o cat.o -o main
(大概是这样,具体记不清了)

#3


你用VC的话就通过Add Project >> Files...来添加相关的程序文件,不要自己新建之后就直接编译。

#4


1楼的正确

#5


如果你没有集成的编译工具,那么就要编写makefile了!具体的语法我不记得了!

#6


Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/112.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

112.exe - 2 error(s), 0 warning(s)

这种错误又指什么?我就这两个错误了@^@,大家加油!

#7


可能是你的某个程序文件找不到main、WinMain之类的函数而导致链接失败。

#8


你没有必要include cat.cpp文件,因为在cat.cpp中已经存在了cat.h,你main.cpp里面又
include了一次,所以重复,解决方法可以修改cat.h
#ifndef CAT_H
#define CAT_H
原来cat.h的内容
#endif
这样就没有问题了,但是你最好去掉#include "cat.cpp",你是用的vc,所以vc会自动帮你
link他们的,如果在linux下面,倒是需要手工分别将其编译成.o,然后再link

#9


楼上正解

#10


Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/112.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

112.exe - 2 error(s), 0 warning(s)

这种错误又指什么?我就这两个错误了@^@,大家加油!

看来你是用VC的win32 application向导写win32 console app了
你建一个空的win32 console app项目,把你的文件添加进去就行了