错误:变量或字段“functionName”声明为无效 我在C++头文件中有如下内容,如下所示。

错误:变量或字段“functionName”声明为无效 我在C++头文件中有如下内容,如下所示。,c++,compiler-errors,header-files,C++,Compiler Errors,Header Files,cmdtree.h: #ifndef CMDTREE_H #define CMDTREE_H #include <iostream> #include <fstream> #include <string> #include "functions.h" // different header where some functions are #include "classes.h" // different header where

cmdtree.h:

#ifndef CMDTREE_H
#define CMDTREE_H

#include <iostream>
#include <fstream>
#include <string>
#include "functions.h"      // different header where some functions are
#include "classes.h"        // different header where classes are
using namespace std;

void printLine(string filename, int line);
void cmd_ask_name();
void cmd_look_suspect();

#endif  /* CMDTREE_H */

您的cmdtree.cpp中似乎缺少以下内容:

void printLine(string filename, int line)
{
    char        descrip[11][500] = {0};
    char        *ch_arr = descrip[0];
    fstream     text;
    int         y = 0;

    cout << "\nfile: " << filename << " line: " << line << endl;

    text.open(filename,ios::in);
    if (!text.is_open()) {         
        cout << "\nCould not open " << filename << "." << endl;
    } else {
        while (!text.eof())
        {
            ch_arr = descrip[y];
            text.getline(ch_arr,500);
            y++;
        }                
    }
    text.close();
    ch_arr = descrip[line];
    cout    << " " << ch_arr << endl;

}

void cmd_ask_name() 
{      

}

void cmd_look_suspect()     // look suspect
{

}
#include "cmdtree.h"
它不会因为与.cpp文件具有相同的根文件名而自动包含


另外,使用名称空间std放置不是一个好主意;在头文件中。在头中显式地使用std::string,然后可以使用名称空间std;在.cpp文件中

您在cmdtree.cpp文件中包含了cmdtree.h,对吗?我用两种方法都包含了cmdtree.h,但仍然得到了编译器错误-头包含在.cpp中,而没有。不幸的是,当我将此包含放在cmdtree.cpp的顶部时,我收到了编译器错误。请更正。我刚刚替换了include,这个特定问题已经解决,但现在它为文件中其他位置声明的结构抛出了未声明的错误。请确保在头文件中或在使用它们之前声明了其他结构。它们是:这是一个重复出现的问题。我会在以后的某个时间发布一个关于这个问题的单独问题。谢谢
#include "cmdtree.h"