Vectors of class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Vectors of class

  1. #1
    Join Date
    Apr 2013

    Vectors of class

    im having a hard time trying to get this vector to work in the class. This is my second class of taking computer science and im really confuse can someone please help

    #include <iostream>
    #include <string>
    #include <vector>

    using namespace std;

    class Course {
    int scores;
    Course(int s);
    int minimum();
    /* void print() {cout << scores; }
    void countA();
    void read();
    int maximum();
    int standard_deviation();
    int average(); */
    scores = 0;
    Course:: Course(int s)

    int Course::minimum()
    int minimum= numbers[0];
    for (int i=0; i<numbers.size(); i++)
    if (numbers[i] <minimum)
    minimum = numbers[i];
    return minimum;
    int main()
    Course cs202;

    vector<int> numbers;
    int grade;

    cout<<"Enter set of scores for students until eof\n";
    cin >> grade;
    while (!cin.eof()) {


    cout << "Minimum = " << cs202.minimum() << endl;
    /*cout << "Average = " << cs202.average() << endl;
    cout << "Standard Deviation = " << cs202.standard_deviation() << endl;
    cout << "Maximum = " << cs202.maximum() << endl;
    cout << "A's Count = " << cs202.countA() << endl;

    cout << "The scores again: \n";


  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Wallisellen (ZH), Switzerland

    Re: Vectors of class

    First, using an external global object within a class (like your vector<int> numbers within Course ) sounds like a bad design.

    Second, what problem do you have with the code you have posted?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: Vectors of class

    When posting code, please format your code before posting and use code tags (Go Advanced, select code and click '#').

    int Course::minimum()
    int minimum = numbers[0];
    numbers is a vector that is local to the function main. Therefore it can't be accessed from class Course. If you want class Course to be able to use numbers, then numbers will need to be made global. However, a better way would be to have vector numbers as a private member of class Course and have additional Course methods to add a score to the vector etc.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain

    C, C++ Compiler: Microsoft VS2017

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This a survey!

HTML5 Development Center