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

Hybrid View

  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
    Hanover Germany

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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 https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.4)

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

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.