Click here to Skip to main content
15,902,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey,
I'm working on vector in c++, and i really don't understand vector very well
i would like to display the following in the terminal

[]
[0 0 0]
error message
error message
[1 2 3]
[1 2 3 ]
[1 0 3 }

here's my Vector.cpp file
C++
#include "Vector.h"
#include <iostream>
#include <vector>

using namespace std;
int main() {

	vector<double> a, b(3), c(3);

a.print ();   //outputs []
b.print ();   // outputs [0 0 0]
c.set(0,-1):   //output error message
c.set(1,0):
c.set(2,1):
c.set(3,2):
c.set(4,3):         /output error message
c.print();           //output [1 2 3]

vector d(c);     
d.print();           //outputs [1 2 3 ] 

d.set(0,1);   
d.print();         // outputs [1 0  3 }

return 0 ; 
}


and here's my Vector.h file

C++
#ifndef VECTOR_H
#define VECTOR_H
#define NULL 0
#include <iostream>

using namespace std;
class Vector
{
public:
	Vector::Vector() {
		size = 0;
		enteries = new int[size];
		for (int k = 0; k < size; k++) {

			enteries[k] = 0;
		}
	}
	Vector::Vector(int s) {
		size = s;
		enteries = new int[size];
		for (int L = 0; L < size; L++) {
			enteries[L] = 0;
		}
	}

	Vector::Vector(const Vector & other) {
		this->size = other.size;
		this->enteries = new int[size];
		for (int i = 0; i < size; i++) {

			this->enteries[i] = other.enteries[i];
		}
	}

	Vector::~Vector() {
		this->enteries = NULL;
	}
	void Vector::print() {
		cout << endl << "[";
		for (int i = 0; i < size; i++) {
			cout << enteries[i] << " ";
		}
		cout << "]" << endl;
	}
	void Vector::set(int val, int pos) {
		if (pos < 0|| pos < size) {
			cout << endl << "Error Message ";
		}
	}


private:
	int size; 
	int *enteries; 
};
#endif


What I have tried:

I have already did the header file, but i really don't know how to display the following after running the program
[]
[0 0 0]
error message
error message
[1 2 3]
[1 2 3 ]
[1 0 3 }
Thank you for your help.
Posted
Comments
Sergey Alexandrovich Kryukov 16-Feb-16 11:11am    
And the problem is..?
—SA
Assam ALzookery 16-Feb-16 11:16am    
i don't know how to finish cpp file to get the result
Thank you
Philippe Mori 16-Feb-16 12:31pm    
Use a debugger. Don't reinvent the wheel and use std::vector instead!

The size checking in set() is wrong and you are not assigning the value:
C++
void Vector::set(int val, int pos) {
    // This is wrong:
    //if (pos < 0|| pos < size) {
    // It should be:
    if (pos < 0 || pos >= size) {
        cout << endl << "Error Message ";
    }
    // This is missing:
    else
        enteries[pos] = val;
}


In your destructor you should delete the allocated memory. Setting the pointer to NULL is useless:
Vector::~Vector() {
    // This is missing:
    delete [] this->enteries;
    // This is useless:
    //this->enteries = NULL;
}
 
Share this answer
 
And in addition to solution 1: If you define Vector in your header file you should use Vector in your cpp file (note the capital V), and not vector!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900