(类)不命名类型

(class) does not name a type

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

我正在尝试通过makefile编译一个程序。当我从命令行启动make时,编译器会给我此错误:

g++ -Wall -g -c main.cpp -std=c++11
In file included from main.cpp:9:0:
athlete.h:9:9: error: 'string' does not name a type
      string name;

我已经尝试搜索,我发现一些常见问题与缺少#include <string>、"倒置预处理器指令"、using namespace std;使用不当或在其他头文件中#includes使用不当有关。我试图修复这 4 点,但没有结果,也许我忽略了一些东西。希望这个问题不会让你感到厌烦。谢谢。在一些文件代码块下面(并非所有文件都包括在内(。

主.cpp

#include <iostream>
#include <fstream>
#include <ctime>
#include <iomanip>
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include "athlete.h"
#include "upd_at.h"
using namespace std;
int main(){
    Athlete f;
    f.set_generalities();
    f.set_perfind();
    f = update_test_res(f);
    return 0;
}

运动员

"....">的意思是:(希望(不相关的代码行。

#ifndef ATHLETE_H_INCLUDED
#define ATHLETE_H_INCLUDED

//declarations of Athlete
class Athlete{
private:
    //generalities
        string name;
        int age;
        int height;
        double weight;
        int tr_exp;
        ....
public:
        ....
};
#endif // ATHLETE_H_INCLUDED

运动员.cpp

void Athlete::set_generalities(){
    string in_name;
    int in_age;
    int in_height;
    double in_weight;
    int in_tr_exp;
    ....
}
void Athlete::set_perfind(){
    int in_RHR, in_maxHR;
    double in_1rmsq, in_1rmbp, in_1rmcl, in_1rmdl, hndgrp;
    ....
    return ;
}
//create a first txt with athlete generalities and tests column
void create_ath_file(){
// current time/date based on current system
     time_t now = time(0);
     tm *ltm = localtime(&now);
     const char* name_pt = name.c_str();
     ofstream myfile;
     myfile.open(name_pt);
     ....
     myfile.close();
}

生成文件

p1: main.o upd_athlete.o athlete.o
    g++ -Wall -g main.o upd_athlete.o athlete.o -o p1 -std=c++11
main.o: main.cpp athlete.h athlete.cpp upd_at.h upd_athlete.cpp
    g++ -Wall -g -c main.cpp -std=c++11
upd_athlete.o: upd_athlete.cpp upd_at.h athlete.cpp athlete.h
    g++ -Wall -g -c upd_athlete.cpp -std=c++11
athlete.o: athlete.cpp athlete.h
    g++ -Wall -g -c athlete.cpp -std=c++11
clean:
    rm *.o

您需要:

  • athlete.h中使用using namespace std;
  • #include "athlete.h"之前将using namespace std;移入main.cpp

现在这些只是非常糟糕的黑客。

  • 有资格string - std::string nameathlete.h. std::在我们打字时并不难打字,所以我不会在任何地方使用using namespace std;

此外,您应该将适当的标准标头直接#includeathlete.h,否则您只是依靠用户和athlete.cppathlete.h包含之前这样做。

使用 std::string 而不是 string。