I get compilation error when i try to compile the following..... Please comment.
Here is my code: ffs.h
#ifndef FFS_H
#define FFS_H
#include <iostream>
#include <string>
#include "commands.h"
class ffs{
private:
Filesystem *filesys;
Superblock block;
void processCommands(InputParser command);
void performQuit();
void performInit(InputParser command);
public:
void acceptCommands();
ffs(){};
};
#endif
ffs.cpp
#include "ffs.h"
void ffs::acceptCommands(){
std::string input;
while(true){
std::cout<< "Enter command : ";
getline(std::cin,input);
InputParser parser(input);
processCommands(parser);
}
}
void ffs::performInit(InputParser command){
command.getCommand().pop_front();
int n = atoi(command.getCommand().front().c_str());
std::cout<< n << " : number of blocks "<<std::endl;
command.getCommand().pop_front();
int m = atoi(command.getCommand().front().c_str());
std::cout<<m << " : number of inode blocks" << std::endl;
command.getCommand().pop_front();
block.ninode=m;
Filesystem fs(n);
filesys = &fs;
}
void ffs::performQuit(){
///filesys->clean();
exit(0);
}
void ffs::processCommands(InputParser command){
std::string cmd=command.getCommandName();
if(cmd.compare(commands::Q())==0) performQuit();
else if (cmd.compare(commands::INIT())==0) performInit(command);
}
tester.h
#ifndef TESTER_H
#define TESTER_H
#include "ffs.h"
class ffs;
class tester{
private:
ffs ffsobj;
public:
void run(){ffsobj.acceptCommands()};
};
#endif
tester.cpp
#include "tester.h"
int main()
{
tester runner;
runner.run();
return 0;
}
ERROR:
g++ -c -Wall tester.cpp
tester.h:7: error: ffs does not name a type
tester.h: In member function void tester::run():
tester.h:9: error: ffsobj was not declared in this scope
tester.h:9: error: expected `;' before ˜} token
make: *** [tester.o] Error 1
Makefile:
CFLAGS=-c -Wall
CC=g++
all: flags.o InputParser.o commands.o Filesystem.o ffs.o tester.o
$(CC) flags.o InputParser.o commands.o Filesystem.o ffs.o tester.o -o runner.o
flags.o : flags.cpp flags.h
开发者_如何学编程 $(CC) $(CFLAGS) flags.cpp
InputParser.o : InputParser.cpp InputParser.h
$(CC) $(CFLAGS) InputParser.cpp
ffs.o: ffs.cpp ffs.h
$(CC) $(CFLAGS) ffs.cpp
commands.o: commands.cpp commands.h
$(CC) $(CFLAGS) commands.cpp
Filesystem.o: Filesystem.cpp Filesystem.h Superblock.h
$(CC) $(CFLAGS) Filesystem.cpp
tester.o: tester.cpp tester.h ffs.o
$(CC) $(CFLAGS) tester.cpp
#fileUtility.o : IFileUtility.h
# gcc -c IFileUtility.h
# Inode.h commands.h Filesystem.h
clean :
rm *.o
You can't use ffs
as your class name, it already has a meaning in c++ (albiet an obscure one). Just pick a different name.
You got a #endif
in your ffs.h
file, without the befinning #if
.
If you include ffs.h
in your tester.h
file, why do you declare class ffs;
?
Error is in following line:
class ffs;
class tester{
private:
ffs ffsobj; // <--- can't declare object for ffs; can be pointer/reference only
To declare an object of ffs
type, class tester
should be able to see its full definition. Otherwise you can just declare a pointer or reference of ffs
type, if you have forward declared like that (ffs*
or ffs&
).
Remove the forward class declaration class ffs;
from tester.h. It is unnecessary since you include ffs.h and then confuses the compiler when it tries to define a class member which it thinks it has not seen the definition for.
do not solve the "does not name a type" problem, but look at this line:
void run(){ffsobj.acceptCommands()};
the correct is:
void run(){ffsobj.acceptCommands();}
精彩评论