未在范围中声明如何修复

时间:2019-09-22 22:28:21

标签: c++ linker-errors

我正在为作业编写代码,该文件读取一个文件,然后将其分类为国内或国际学生,并在其中显示姓名,分数和ID。 但是我一直收到链接器错误,而且我不知道问题出在哪里。 (很抱歉,我不知道如何在此处添加语法突出显示,但是我很想完成)

文件student.hpp

//header file student.hpp to declare your classes
//use namespace std

#include <string> //you will have to use string in C++
#include <iostream>
#pragma once
#ifndef STUDENT_H
#ifndef DOMESTICSTUDENT_H
#ifndef INTERNATIONALSTUDENT_H
#define STUDENT_H
#define DOMESTICSTUDENT_H
#define INTERNATIONALSTUDENT_H

using namespace std;
class Student
{
public:
  Student(string firstName, string lastName, float cgpa, int researchScore, int id);
  Student();
private:
  void check_id();
};    

class DomesticStudent
{
public:
  DomesticStudent(string province);
  DomesticStudent();
};

class InternationalStudent
{
public:
  InternationalStudent();
  string country;
  int totalScore;
  int TotalScore(int reading, int listening, int writing, int speaking);
};


#endif //ends #ifndef
#endif 
#endif

文件student.cpp

//student.cpp to implement your classes
#include <iostream>
#include <fstream> //file processing
#include <sstream> //formatted string processing
#include <cstdlib> //atof and ato
#include <string>
#include "student.hpp"

using namespace std;

void Student::check_id()
{
  int id= 2021500;
  if (id < 20200000 || id>20210000)
    {
        cout << "Invalid ID. Restart the program";
        return;
    }
}

int InternationalStudent::TotalScore(int reading, int listening, int speaking, int writing)
{
totalScore= reading + listening + speaking + writing;
    return totalScore;
}
class ToeflScore: public InternationalStudent
{
public:
  ToeflScore(int reading, int listening, int writing, int speaking);
  ToeflScore();
  int get_reading(int reading);
  int get_listening(int listening);
  int get_speaking(int speaking);
  int get_writing(int writing);
  void check_score(int reading, int listening, int speaking, int writing);
};    

int ToeflScore::get_reading(int reading)
{
    return reading;
}

int ToeflScore::get_listening(int listening)
{
    return listening;
}

int ToeflScore::get_speaking(int speaking)
{
    return speaking;
}

int ToeflScore::get_writing(int writing)
{
    return writing;
}

void ToeflScore::check_score(int reading, int listening, int speaking, int writing)
{
  if (reading < 0 || reading>30)
    {
        cout << "Invalid reading score. Re-enter.";
        return;
    }
  if (listening < 0 || listening>30)
    {
        cout << "Invalid listening score. Re-enter.";
        return;
    }
  if (speaking < 0 || speaking>30)
    {
        cout << "Invalid speaking score. Re-enter.";
        return;
    }
  if (writing < 0 || writing>30)
    {
        cout << "Invalid reading score. Re-enter.";
        return;
    }
}

文件main.cpp

//main.cpp, put your driver code here, 
//you can manipulate your class objects here
#include <iostream> //cin and cout
#include <fstream> //file processing
#include <sstream> //formatted string processing
#include <cstdlib> //atof and ato
#include <string>
#include "student.hpp"

using namespace std;

int main() {

    ToeflScore score;
    InternationalStudent IS;

    string line;
    ifstream domesticFile("domestic-stu.txt");
    if (!domesticFile.is_open()) {
        cout << "Unable to open file domestic-stu.txt" << endl;
        return -1;
    }

    getline(domesticFile, line);
    cout << "File format: " << line << endl;    

    int stu_count = 1;
    while (getline(domesticFile, line)) {

        istringstream ss(line);

        string firstName, lastName, province, s_cgpa, s_researchScore;
        float cgpa;
        int researchScore;

        getline(ss, firstName, ',');    
        getline(ss, lastName, ',');
        getline(ss, province, ',');
        getline(ss, s_cgpa, ',');
        cgpa = atof(s_cgpa.c_str());
        getline(ss, s_researchScore, ',');
        researchScore = atoi(s_researchScore.c_str());

        cout << "Domestic student " << stu_count << " " << firstName << " "
            << lastName << " from " << province << " province has cgpa of "
            << cgpa << ", and research score of " << researchScore << endl;

        stu_count++;
    }

    ifstream internationalFile("international-stu.txt");
    if (!internationalFile.is_open()) {
        cout << "Unable to open file international-stu.txt" << endl;
        return -2;
    }

    string line2;
    getline(internationalFile, line2);
    cout << "File format: " << line2 << endl;

    int int_count = 1;
    while (getline(internationalFile, line2)) {

        istringstream ss(line2);

        string firstName, lastName, country, s_cgpa, s_researchScore;
        float cgpa;
        int researchScore;    

        getline(ss, firstName, ',');
        getline(ss, lastName, ',');
        getline(ss, country, ',');
        getline(ss, s_cgpa, ',');
        cgpa = atof(s_cgpa.c_str());

        getline(ss, s_researchScore, ',');
        researchScore = atoi(s_researchScore.c_str());

        int reading;
        int listening;
        int speaking;
        int writing;

        reading = score.get_reading( reading);
        listening = score.get_listening(listening);
        speaking = score.get_speaking(speaking);
        writing = score.get_writing(writing);

        score.check_score(reading,listening,speaking, writing);
        IS.TotalScore(reading, listening, speaking, writing);

        cout << "International student " << stu_count << " " << firstName << " "
            << lastName << " from " << country << " province has cgpa of "
             << cgpa << ", and research score of " << researchScore << "and a ToeflScore of"<< IS.totalScore<< endl;

        int_count++;
    }

    //close your file
    domesticFile.close();
    internationalFile.close();

    return 0;
}

这是我得到的错误

main.cpp: In function ‘int main()’:
main.cpp:23:2: error: ‘ToeflScore’ was not declared in this scope
  ToeflScore score;
  ^
main.cpp:23:13: error: expected ‘;’ before ‘score’
  ToeflScore score;
             ^
main.cpp:114:13: error: ‘score’ was not declared in this scope
   reading = score.get_reading( reading);
             ^

1 个答案:

答案 0 :(得分:1)

问题

main.cpp中,您包括标准库文件和students.hpp

在此包含的标题中,您仅定义了类StudentInternationalStudenDomesticStudent。其中没有ToeflScore的痕迹。因此,当您使用该(未定义)类时,编译器(而不是链接程序)会抱怨。

解决方案

您已在ToeflScore中定义了类students.cpp。将该类定义移至students.hpp。然后它应该起作用(其余错误似乎仅与第一个错误有关)。