繁体   English   中英

C ++中的对象和函数

[英]Objects and functions in C++

我正在研究将输入字符串的文件并根据用户输入文件组织它们的工作。 输入文件的顶部是其组织方式,其余部分是格式化的信息,每条信息都位于其自己的一行上以馈入对象。 我的对象需要的每条信息都分成5行。

我的问题是打印函数,该函数将在对象指针数组上调用,并在调用时传递用户排序首选项。 到目前为止,我发现我什至无法调用对象上的函数。 我尝试了很多不同的解决方案,例如创建一个全新的对象(Object myObject();)并在其上调用相同的打印函数,但这也不起作用。 有没有办法在指向我创建的对象的指针数组上调用该函数并将其传递给该首选项变量? 我试图添加一个默认的构造函数并在其上调用该函数,但是当我这样做时,object.cpp中的两个测试(在调用print时退出变量)表明变量可能作为NULL传入。

main.cpp中

#include <iostream>
#include <string>
#include "object.h"

int main()
{
    Object **ptr = new Object*[5];//Create array of object pointers

    string firstLine, secondLine, thirdLine, userPreference;
    float fourthLine;
    int fifthLine;

    string currentLine;
    int lineCounter = 0;
    int arrayCounter = 0;

    getline(cin, userPreference);//Preference on sorting method

    while(getline(cin, currentLine)) {//Main loop
        switch(lineCounter) {
            case(1):
                firstLine = currentLine;
                lineCounter++;
            break;

            case(2):
                secondLine = currentLine;
                lineCounter++;
            break;

            case(3):
                thirdLine = currentLine;
                lineCounter++;
            break;

            case(4):
                fourthLine = stof(currentLine);//Get float value of currentLine and set fourthLine equal to it
                lineCounter++;
            break;

            case(5):
                fifthLine = stoi(currentLine);//Get integer value of currentLine and set fithLine equal to it
                ptr[arrayCounter] = new Object(firstLine, secondLine, thirdLine, fourthLine, fifthLine);
                arrayCounter++;//Step to next element now that the current element is filled
                lineCounter = 0;//Reset lineCounter
                cout << "case 5 test" << endl;
            break;
        }//End switch
    }

    ptr -> print(userPreference);

    for(int i = 0; i < arrayCounter; i++) {//Delete everything
        cout << "Test";
    }
}

object.cpp

#include "object.h"
#include <iostream>

using namespace std;

Object::Object(string title, string URL, string comment, float length, int rating) {
    vidTitle = title;
    vidURL = URL;
    vidComment = comment;
    vidLength = length;
    vidRating = rating;
}

void Object::print(string preference) {
    vidPreference = preference;
    cout << vidPreference;
    cout << preference;
    if(vidPreference == "rating") {
        //Rating sort
        //Is the array passed through correctly? If so, can I climb through like I do to assign it in main?
    }

    if(vidPreference == "length") {
        //Length sort
    }

    if(vidPreference == "title") {
        //Title sort
    }
    else {
        cout << preference << " is not a legal sorting method, giving up." << endl;
    }
}

object.h

#ifndef OBJECT_H
#define OBJECT_H

#include <string>

using namespace std;

class Object
{

    public:
        Object(string title, string URL, string comment, float length, int rating);
        void print(string preference);
    private:
        string vidTitle, vidURL, vidComment, vidPreference;
        float vidLength;
        int vidRating;
};

#endif

用于

ptr -> print(userPreference);

如果ptrObject*类型,则将是有效的。 由于它是Object**类型的,因此您需要使用一个计算为Object*的表达式。 例如,

for(int i = 0; i < arrayCounter; i++)
{
   ptr[i]->print(userPreference);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM