当前位置: 技术问答>linux和unix
在linux下创建一个类怎么出现会出现这种错误,我感觉是没有错误的,那位高手过来看看
来源: 互联网 发布时间:2015-10-13
本文导语: 下面文件有KenoComm.h ,KenoComm.c ,b.c三个文件,还有Makefile文件. 类CKenoComm定义在KenoComm.c中,b.c调用了类CKenoComm. 我在linux下编译时,问题是 [root@localhost 7.12]# make cc -c -o b.o b.c In file included from b.c:5: KenoComm.h:14: p...
下面文件有KenoComm.h ,KenoComm.c ,b.c三个文件,还有Makefile文件.
类CKenoComm定义在KenoComm.c中,b.c调用了类CKenoComm.
我在linux下编译时,问题是
[root@localhost 7.12]# make
cc -c -o b.o b.c
In file included from b.c:5:
KenoComm.h:14: parse error before "CKenoComm"
KenoComm.h:15: syntax error before '{' token
KenoComm.h:19: warning: data definition has no type or storage class
KenoComm.h:20: parse error before '~' token
b.c: In function `main':
b.c:9: parse error before "a"
b.c:10: `a' undeclared (first use in this function)
b.c:10: (Each undeclared identifier is reported only once
b.c:10: for each function it appears in.)
make: *** [b.o] Error 1
那位大侠能帮我解决一下吗,先谢谢了.
我的新建类的头文件是KenoComm.h
// KenoComm.h: interface for the CKenoComm class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_KENOCOMM_H__3781E982_B927_4046_B209_1D8827A17071__INCLUDED_)
#define AFX_KENOCOMM_H__3781E982_B927_4046_B209_1D8827A17071__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include
class CKenoComm
{
public:
void DelPipe();
int ReadPipe();
CKenoComm();
virtual ~CKenoComm();
};
#endif // !defined(AFX_KENOCOMM_H__3781E982_B927_4046_B209_1D8827A17071__INCLUDED_)
我的新建类的.cpp文件为KenoComm.c
// KenoComm.c: implementation of the CKenoComm class.
//
//////////////////////////////////////////////////////////////////////
//#include "stdafx.h"
#include "KenoComm.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CKenoComm::CKenoComm()
{
}
CKenoComm::~CKenoComm()
{
}
int CKenoComm::ReadPipe()
{
printf("ReadPipen");
return 1;
}
void CKenoComm::DelPipe()
{
printf("DelPipen");
}
我的主文件为 b.c
#include "KenoComm.h"
#include
int main(int argc, char* argv[])
{
CKenoComm a;
a.DelPipe();
a.ReadPipe();
printf("Hello World!n");
return 0;
}
我的Makefile文件为
RM = rm -f
CXX = g++
#CXXFLAGS = +DA2.0W -s
CXXFLAGS = +O3
Pipe: PipeComm
OBJS_PipeComm = b.o KenoComm.o
PipeComm : $(OBJS_PipeComm)
$(RM)$@
$(CXX) -o $@ $(CXXLDFLAGS) $(OBJS_PipeComm)
clean:
$(RM) core PipeComm* *.o
~
~
类CKenoComm定义在KenoComm.c中,b.c调用了类CKenoComm.
我在linux下编译时,问题是
[root@localhost 7.12]# make
cc -c -o b.o b.c
In file included from b.c:5:
KenoComm.h:14: parse error before "CKenoComm"
KenoComm.h:15: syntax error before '{' token
KenoComm.h:19: warning: data definition has no type or storage class
KenoComm.h:20: parse error before '~' token
b.c: In function `main':
b.c:9: parse error before "a"
b.c:10: `a' undeclared (first use in this function)
b.c:10: (Each undeclared identifier is reported only once
b.c:10: for each function it appears in.)
make: *** [b.o] Error 1
那位大侠能帮我解决一下吗,先谢谢了.
我的新建类的头文件是KenoComm.h
// KenoComm.h: interface for the CKenoComm class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_KENOCOMM_H__3781E982_B927_4046_B209_1D8827A17071__INCLUDED_)
#define AFX_KENOCOMM_H__3781E982_B927_4046_B209_1D8827A17071__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include
class CKenoComm
{
public:
void DelPipe();
int ReadPipe();
CKenoComm();
virtual ~CKenoComm();
};
#endif // !defined(AFX_KENOCOMM_H__3781E982_B927_4046_B209_1D8827A17071__INCLUDED_)
我的新建类的.cpp文件为KenoComm.c
// KenoComm.c: implementation of the CKenoComm class.
//
//////////////////////////////////////////////////////////////////////
//#include "stdafx.h"
#include "KenoComm.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CKenoComm::CKenoComm()
{
}
CKenoComm::~CKenoComm()
{
}
int CKenoComm::ReadPipe()
{
printf("ReadPipen");
return 1;
}
void CKenoComm::DelPipe()
{
printf("DelPipen");
}
我的主文件为 b.c
#include "KenoComm.h"
#include
int main(int argc, char* argv[])
{
CKenoComm a;
a.DelPipe();
a.ReadPipe();
printf("Hello World!n");
return 0;
}
我的Makefile文件为
RM = rm -f
CXX = g++
#CXXFLAGS = +DA2.0W -s
CXXFLAGS = +O3
Pipe: PipeComm
OBJS_PipeComm = b.o KenoComm.o
PipeComm : $(OBJS_PipeComm)
$(RM)$@
$(CXX) -o $@ $(CXXLDFLAGS) $(OBJS_PipeComm)
clean:
$(RM) core PipeComm* *.o
~
~
|
RM = rm -f
CXX = g++
#CXXFLAGS = +DA2.0W -s
CXXFLAGS = +O3
Pipe: PipeComm
OBJS_PipeComm = b.c KenoComm.c
PipeComm : $(OBJS_PipeComm)
$(RM) $@
$(CXX) -o $@ $(CXXLDFLAGS) $(OBJS_PipeComm)
clean:
$(RM) core PipeComm* *.o
CXX = g++
#CXXFLAGS = +DA2.0W -s
CXXFLAGS = +O3
Pipe: PipeComm
OBJS_PipeComm = b.c KenoComm.c
PipeComm : $(OBJS_PipeComm)
$(RM) $@
$(CXX) -o $@ $(CXXLDFLAGS) $(OBJS_PipeComm)
clean:
$(RM) core PipeComm* *.o
|
你是c++程序,文件用.cpp。你用.c,编译器认为是c程序,c程序里没有class。
|
文件名改为.C或.cpp后缀,或者改makefile用-x c++编译选项指定用从c++编译器来编译.c文件
|
你用什么编辑的程序?看看是DOS格式的还是UNIX合适的?
字符好象不对。
字符好象不对。
|
我遇到过这样的问题,不过忘记怎么解决的了!不好意思,嘿嘿!
好象如楼上所说的,字符格式不对.重新输入一次试试.
好象如楼上所说的,字符格式不对.重新输入一次试试.
|
程序没问题,makefile写错了
|
难道是要把.c改为.cpp?