FFS 不会在 C++ 中命名类型

ffs does not name a type in C++

本文关键字:类型 C++ FFS      更新时间:2023-10-16

当我尝试编译以下内容时出现编译错误.....请评论。

这是我的代码: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);
}

测试人员.h

#ifndef TESTER_H
#define TESTER_H
#include "ffs.h"
class ffs;
class tester{
private: 
    ffs ffsobj;
public:
    void run(){ffsobj.acceptCommands()};
};
#endif

测试仪.cpp

#include "tester.h"
int main()
{
    tester runner;
    runner.run();
    return 0;
}

错误:

  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

制作文件:

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

你不能使用ffs作为你的类名,它在 c++ 中已经有了一个含义(albiet 是一个晦涩难懂的)。只需选择一个不同的名称即可。

您在ffs.h文件中得到了一个#endif,没有 befinning #if

如果在tester.h文件中包含ffs.h,为什么要声明class ffs;

错误在以下行中:

class ffs;
class tester{
private: 
    ffs ffsobj; // <--- can't declare object for ffs; can be pointer/reference only

若要声明ffs类型的对象,class tester应该能够看到其完整定义。否则,您可以只声明ffs类型的指针或引用,如果您像这样向前声明(ffs*ffs&)。

从 tester.h 中删除前向类声明class ffs;。 这是不必要的,因为您包含 ffs.h,然后在编译器尝试定义它认为它没有看到定义的类成员时混淆编译器。

解决"不命名类型"的问题,但请看这一行:

void run(){ffsobj.acceptCommands()};

正确的是:

void run(){ffsobj.acceptCommands();}