对如何定义/实现原型感到困惑

时间:2018-07-05 23:46:55

标签: c++ reference prototype

我正在从事一项工作,该工作为我们提供了代码框架,并要求我们进行某些更改并对某些内容进行编程以对其进行更改。现在,我只是尝试实现给定的原型功能之一,称为“ readConfig”,以便用户可以输入每个所需的输入。我不断收到错误消息:“对`readConfig(int&,int&,std :: __ cxx11 :: basic_string,std :: allocator>&)的未定义引用”。

我已经四处搜寻,看来这个错误是由于没有定义原型。从课程材料中,我仍然不确定如何正确定义/实现原型函数以正确引用它。我不明白确切的实现要求什么。请注意,我并不是在要求任何人做作业,而只是在学习如何正确定义/使用原型和函数时寻求帮助。请告诉我是否可以提供其他信息。这是我的代码:

/*
* DON'T CHANGE FOLLOWING CODE
*/
#include "SFML/Graphics.hpp"
#include <iostream>
#include <fstream>
#include <string>

const float WIDTH     = 550.0;     // width of the window for display
const float HEIGHT    = 750.0;     // height of the window for display
const int   FONT_SIZE   =  25;     // character size
const float LEFT_X    = 100.0;     // Left X position for label
const float LOW_Y     = 700.0;     // LOW  Y position for label
const float HIGH_Y    =  50.0;     // HIGH Y position for label

using namespace std;

// Function prototypes - you need to implement
void readConfig(int &numGrades, int &maxInGrade, string &filename);
int  findNumInRange(string filename, int low, int high);
void initFrequency(string letters[], int freqLetters[],
               int numGrades, string filename);
void setCurves(sf::RectangleShape bars[], sf::Text labels[], int freqLetters[],
           string letters[], sf::Font &font, int numGrades, int maxInGrades);

/* DON'T CHANGE THE ABOVE CODE */

int main() {

int numGrades  = 5;   // number of different letter grades
int maxInGrade = 20;  // maximum number of students in any letter grade
string filename;      // name of input file containing scores

// STEP 1:
// IMPLEMENT readConfig that initializes (1) numGrades, (2) maxInGrade, and
//                                       (3) filename
//
readConfig (numGrades, maxInGrade, filename);
cout << "Enter the maximum number of letter grades:";
cin >> numGrades;
cout << "Enter the anticipated maximum number of students receiving the same letter grade:";
cin >> maxInGrade;
cout << "Enter the name of the file containing scores:";
cin >> filename;
string letters[numGrades];   // each entry represents string representing the grade
int freqLetters[numGrades];  // each entry records occurrence of grades corresponding
                             // to the letter grade

// STEP 2:
// IMPLEMENT initFrequency that initializes letters[] and freqLetters[] by processing
//                              input file (filename).
// You need to replace the following for loop with a call to initFrequency
//
initFrequency (letters, freqLetters, numGrades, filename);

//
// DON'T CHANGE FOLLOWING CODE
//
sf::RenderWindow window(sf::VideoMode(600, 800), "Grade Histogram");
sf::Font font;
font.loadFromFile("resources/arial.ttf");
/* DON'T CHANGE ABOVE CODE */

// You are free to change the following code. However, as it is, it should work
sf::Text min, max;
min.setFillColor(sf::Color::Yellow);
min.setFont(font);
min.setString("0");
min.setCharacterSize(FONT_SIZE);
max.setFillColor(sf::Color::Yellow);
max.setFont(font);
max.setString(to_string(maxInGrade));
max.setCharacterSize(FONT_SIZE);
min.setPosition(LEFT_X-FONT_SIZE*2, LOW_Y-FONT_SIZE);
max.setPosition(LEFT_X-FONT_SIZE*2, HIGH_Y-FONT_SIZE/2);

sf::RectangleShape bars[numGrades];
sf::Text labels[numGrades];

// STEP 3:
// IMPLEMENT setTheCurves and replace the following initialization
//                            code with a call to setTheCurves
//
float x_incr = (WIDTH - 100)/numGrades;
float x_offset = LEFT_X+FONT_SIZE;
float height_unit_pixels = (HEIGHT - (HIGH_Y + FONT_SIZE)) / maxInGrade;

int count = 0;
for (int i=0; i < numGrades; i++) {
    float height = freqLetters[i] > maxInGrade ? maxInGrade : freqLetters[i];
    bars[i].setSize(sf::Vector2f(x_incr-50, height*height_unit_pixels));
    bars[i].setPosition(x_offset+x_incr/2, LOW_Y);
    bars[i].rotate(180);
    labels[i].setFillColor(sf::Color::Yellow);
    labels[i].setFont(font);
    labels[i].setString(letters[i]);
    labels[i].setCharacterSize(FONT_SIZE);
    labels[i].setFillColor(sf::Color::Red);
    labels[i].setPosition(bars[i].getPosition().x-FONT_SIZE, LOW_Y+FONT_SIZE);
    x_offset += x_incr;
}

/* DON'T CHANGE THE FOLLOWING CODE */
while (window.isOpen()) {
    sf::Event event;

    while (window.pollEvent(event)) {
        switch (event.type) {
            case sf::Event::Closed:
                window.close();
                break;
            default:
                window.clear();
                window.draw(min);
                window.draw(max);

                for (int i = 0; i < numGrades; i++) {
                    window.draw(bars[i]);
                    window.draw(labels[i]);
                }
                window.display();
        }
    }
}
return 0;
}

0 个答案:

没有答案