0

I'm pretty new to C++.

I'm having an issue where my class constructor seemingly can't initialize a vector class member. The constructor would need to read a file, collect some data and then resize the vector during run time.

I created a simpler example to focus on the issue.

Here's the header file (test.h):

// File Guards
#ifndef __TEST_H
#define __TEST_H

// Including necessary libraries
#include <vector>

// Use the standard namespace!
using namespace std;

// Define a class
class myClass
{
    // Public members
    public:

        // vector of integers 
        vector<int> vec;

        // Declare the constructor to expect a definition
        myClass();
};

// Ends the File Guard
#endif

And here's the source file:

// Including the necessary headers
#include <iostream>
#include "test.h"

// Defining the constructor
myClass::myClass()
{
    // Loop control variable
    int i;

    // For loop to iterate 5 times
    for (i = 0; i < 5; i++)
    {
        // Populating the vector with 0s
        vec.push_back(0);
    }
}

// Main function to call the constructor
int main()
{
    // Create a "myClass" object
    myClass myObject;

    // iterating through the vector class member
    for (int x : myObject.vec)
    {
        // Outputting the elements
        cout << x + " ";
    }

    // Return statement for main function
    return 0;
}

I'd expect five 0's to be printed, but instead, nothing happens. I've thought about this for awhile and haven't found a resolution yet. Any ideas as to what's happening here?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ryan
  • 1
  • 1
  • Typo? `cout << x << " "` instead of `cout << x + " "`? – Some programmer dude Apr 21 '20 at 06:55
  • As a side note, debugging could have helped you here – Ap31 Apr 21 '20 at 06:56
  • On an unrelated note, all symbols (even preprocessor macros) beginning with double underscore are *reserved*. You should never define such symbols yourself. See e.g. [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for more information. – Some programmer dude Apr 21 '20 at 06:58

1 Answers1

1

Looks like the problem is in this line

cout << x + " ";

You should not add x with a space.

It should be cout << x << " ";

artm
  • 17,291
  • 6
  • 38
  • 54
  • Thanks for the quick response! That works. Though I'm not positive it will work when I apply it to the larger program. I'll test it. – Ryan Apr 21 '20 at 07:02
  • 1
    @Ryan Please go back to your text book or tutorial and read about writing output to `cout` again. If it tells you to use `x + " "` to print an integer followed by a space then I'd be very surprised. The expression `x + " "` will result in a string, but it will be an invalid string and lead to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). – Some programmer dude Apr 21 '20 at 07:07
  • Yeah, that was my mistake with the bad syntax. Question though, any opinions on the amount of commenting? I basically use a comment on every line of my programs. – Ryan Apr 21 '20 at 07:27
  • @Ryan In my opinion there is too much commenting. I would love to give an explanation, but doing so in this comment on this answer wouldn't work, and posting a new answer would be off-topic for your original question. You *might* post a new question here on SO, but it might be closed as too "opinion based". --- So - could I suggest posting your code and asking about its commenting on https://codereview.stackexchange.com/ --- many of the people there are the same helpful people here on SO. – Allison Lock Apr 21 '20 at 08:36
  • @Ryan Regarding the commenting, for a statement like `myClass myObject;` it's pretty clear what it does, adding a comment like `// Create a "myClass" object` just tells us what we can already clearly see. Comments for code that is hard to follow, or for explanation about why you choose one solution over possible others, or what complicated function do and what they might return, and what the arguments are fore, those are good comments. – Some programmer dude Apr 26 '20 at 20:43