简体   繁体   中英

Objects and functions in C++

I'm working on something that will take in an input file of strings and organize them based on a user input file. The top of the input file is the way it's organized and the rest is formatted information, each on it's own line to take in to feed into an object. Each individual piece of information that's needed for my object is divided into 5 consecutive lines.

My problem is the print function, which would be called on the array of object pointers and pass in the user sort preference when called. What I've found so far is that I am unable to to even call the function on the object. I've tried a bunch of different solutions like creating an entirely new object (Object myObject();) and calling the same print function on it but that won't work either. Is there a way to call the function on the array of pointers to objects that I created and pass in that preference variable? I've tried adding a default constructor and calling the function on that, but when I did it the two tests in object.cpp that cout the variables when print is called show that the variables are passed in as NULL possibly.

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

Use of

ptr -> print(userPreference);

would be valid had ptr been of type Object* . Since it is of type Object** , you need to use an expression that evaluates to Object* . For instance,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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