Hey ..could u plz check wts wrong with my program

im studying data structuer using c++ in my 1st year
i wrote a c++ programme using class called vector for hangling n-dimrnsional vector..
but i had errors when compiling i don't know why & whats the right solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127




#include<iostream>
#include<math.h>
using namespace std;

class vector {

private:int size;
		int index;
		double *array;

public:vector();
	   vector(int n);
	   ~vector();
	   double get_length(int c);
	   void read_vector(int c);
	   void print_vector(int c);
	   vector operator +(vector m);
       vector operator -(vector m);
       double operator *(vector m);
}

/*   constructers   */

vector::vector(){
	size=1;
	index=0;
	array=new double[size];
}



vector::vector(int n){
	size=n;
	index=0;
	array=new double[size];
}  

/*   destructers   */
vector::~vector(){
	delete []array;
}

/*   funcs  */
double vector::get_length(int c){
	double sum=0;
	for(int i=0;i<c;i++)sum+=pow (array[i],2);
	
	cout<<sqrt(sum);
}
void vector::read_vector(int c){cout<<"enter vector elements"<<endl;
	for(int j=0;j<c;j++)cin>>array[j];}


void vector::print_vector(int c){
for(int t=0;t<c;t++)cout<<array[t]<<"\n";
}

vector vector::operator + (vector m){
	vector tmp;
	if(size==m.size){
		for(int ir=0;ir<size;ir++){

			tmp[ir]= array[ir]+m[ir];
			
			return tmp;}
	}
	else cout<<"not possible"<<endl;
}

/*    operaters   */

vector vector::operator-(vector m){
	vector tmp;
	if(size==m.size){
		for(int ir=0;ir<size;ir++){

			tmp[ir]= array[ir]-m[ir];
			
			return tmp;}
	}
	else cout<<"not possible"<<endl;
}



double vector::operator+(vector m){
    double pro,product;
	if(size==m.size){
		for(int ir=0;ir<size;ir++){

			pro = array[ir]*m[ir];product+=pro;}
			
			return product;
	}
	else cout<<"not possible"<<endl;
}

/*    main 4 check  */

int main(){

	vector v1(5);
double w;
	v1.get_length(5);

	vector x1(7);
	vector x2(7);
	x1.read_vector(7);
	x2.read_vector(7);
	vector x3(8);
    vector x4(10),x5(10),x6(10);
    
	x4=x1+x2;
	x5=x1-x2;
    w=x1*x2;

    cout<<w<<endl;
	x1.print_vector(7);
	x5.print_vector(10);

	return 0;
}
Last edited on
It would help if you told us what are these errors of which you speak.

I would like to warn you that you're coming very close to having a naming conflict with the class "vector" in the C++ standard library. The only thing that's stopping you from having it (as far as I can see) is the fact that you didn't #include <vector>. :/

-Albatross
Last edited on
thx for replying ..here's the errors

--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
C:\HW\1.cpp(29) : error C2533: 'vector::vector' : constructors not allowed a return type

C:\HW\1.cpp(64) : error C2264: 'vector::vector' : error in function definition or declaration; function not called

C:\HW\1.cpp(68) : error C2676: binary '[' : 'class vector' does not define this operator or a conversion to a type acceptable to the predefined operator

C:\HW\1.cpp(68) : error C2676: binary '[' : 'class vector' does not define this operator or a conversion to a type acceptable to the predefined operator

C:\HW\1.cpp(78) : error C2264: 'vector::vector' : error in function definition or declaration; function not called

C:\HW\1.cpp(82) : error C2676: binary '[' : 'class vector' does not define this operator or a conversion to a type acceptable to the predefined operator

C:\HW\1.cpp(82) : error C2676: binary '[' : 'class vector' does not define this operator or a conversion to a type acceptable to the predefined operator

C:\HW\1.cpp(91) : error C2556: 'double __thiscall vector::operator +(class vector)' : overloaded function differs only by return type from 'class vector __thiscall vector::operator +(class vector)'

C:\HW\1.cpp(22) : see declaration of '+'

C:\HW\1.cpp(91) : error C2371: '+' : redefinition; different basic types

C:\HW\1.cpp(22) : see declaration of '+'

C:\HW\1.cpp(91) : error C2084: function 'class vector __thiscall vector::operator +(class vector)' already has a body

C:\HW\1.cpp(96) : error C2676: binary '[' : 'class vector' does not define this operator or a conversion to a type acceptable to the predefined operator

C:\HW\1.cpp(118) : error C2264: '+' : error in function definition
or declaration; function not called

C:\HW\1.cpp(118) : error C2088: '+' : illegal for class

Error executing cl.exe.

1.exe - 13 error(s), 0 warning(s)
Hmm. cl.exe has a funny way of saying "missing a semicolon after the class declaration".

That aside, you forgot to define a few operators. You forgot to define operator[] for use in your operator definitions. Also, do you have a typo on line 90? It looks as though you have one character that's not quite what it's supposed to be. :)

-Albatross
Last edited on
Your first problem is very simple ;-), class vector is not completed with "};"
Albatross & EricDu thank u very much ..

i carried out what u noticed me ..but i still have this strange error i don't know wts wrong now
& wt u said about "define operator[] for use in your operator " i did'nt take any thing about that in university ..so i didn't know wt u meant ..

here's the error's i had - !!!!!

--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
C:\HW\1.cpp(72) : error C2676: binary '[' : 'class vector' does not define this operator or a conversion to a type acceptable to the predefined operator

C:\HW\1.cpp(72) : error C2676: binary '[' : 'class vector' does not define this operator or a conversion to a type acceptable to the predefined operator

C:\HW\1.cpp(86) : error C2676: binary '[' : 'class vector' does not define this operator or a conversion to a type acceptable to the predefined operator

C:\HW\1.cpp(86) : error C2676: binary '[' : 'class vector' does not define this operator or a conversion to a type acceptable to the predefined operator

C:\HW\1.cpp(101) : error C2676: binary '[' : 'class vector' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.

1.exe - 5 error(s), 0 warning(s)
The above errors mean that you wrote codes to call function of vector::operator[](), but you don't define it yet. For example in line 72, you wrote "tmp[ir]= array[ir]+m[ir]", both tmp and m are vector type, but vector doesn't have operator[] defined. To define it, just like to define other operators such as operator+, operator- etc.
ohh ..now i understand that one ..merci eric
:)

Topic archived. No new replies allowed.