Vectors

This is probably pretty basic, but I'm having a problem with vectors. I have a vector of an object, and when I try to access the vector, my program crashes.

The object looks like this:

Color.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef _COLOR_H_
	#define _COLOR_H_

class Color {
	public:
		Color(double, double, double);
		double r, g, b;
		static Color BLACK, RED, GREEN, BLUE, WHITE;
};

#endif 


Color.cpp
1
2
3
4
5
6
7
#include "Color.h"

Color::Color(double r, double g, double b) {
	this->r = r;
	this->g = g;
	this->b = b;
}


And I make my vector like this:

std::vector<Color> colors;

I have a custom operator that adds the colors. The operator looks like this:

operator<<
1
2
3
4
5

Plane Plane::operator<<(const Color &c) {
	this->colors.push_back(c);
	return *this;
}


And is used like this:

plane << Color(0, 0, 1);

The actual code I use is a little more complex, so I'll expand as need be.

Anyway, when I try to access a color like so:

colors[0].r

My program crashes. Is it something conceptually that I'm doing wrong, or could it be something in other parts of my code. I have narrowed it down to the above line causing the program to crash, as when I comment it out, the program is fine (except that I can't access my Colors).
Last edited on
After further analysis, I've found the issue. It wasn't with my vector, but with a custom operator. I needed to add an ampersand ( & ) to it (&operator<<)
Topic archived. No new replies allowed.