no match for 'operator[]' in 'temp[0]'

**NEW CODE SINCE LAST ANSWER**(changed all multidimensional array to single)
ANY other suggestions or help is appreciated!

Hai,
I got this error going in my .ccp.(no match for 'operator[]' in 'temp[0]') I have been trying to figure this out for awhile but can't. I know my code isnt finished but without finding what is wrong with this right now i can't see what else i have to do.

My objective:
-add, subtract and multiply matrices
-overload operators


.cpp

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
#include <cstdlib>
#include <iostream>
#include "matrix.h"


//constructors
matrix::matrix(){
    int matrix1[9];
	int temp[9];
	
};

void matrix::setvalues(int matrix[]){
     
     for(int i = 0; i < 8; i++){
             matrix1[i] = matrix[i];
     };
   
};

matrix operator+(const matrix m1, const matrix m2){
	matrix temp;
	temp[0] = m1[0]+m2[0];
	temp[1] = m1[1]+m2[1];
    temp[2] = m1[2]+m2[2];
    temp[3] = m1[3]+m2[3];
    temp[4] = m1[4]+m2[4];
    temp[5] = m1[5]+m2[5];
    temp[6] = m1[6]+m2[6];
	temp[7] = m1[7]+m2[7];
	temp[8] = m1[8]+m2[8];
	
	return temp;
};

matrix operator-(const matrix m1, const matrix m2){
	matrix temp;
    temp[0] = m1[0]-m2[0];
	temp[1] = m1[1]-m2[1];
    temp[2] = m1[2]-m2[2];
    temp[3] = m1[3]-m2[3];
    temp[4] = m1[4]-m2[4];
    temp[5] = m1[5]-m2[5];
    temp[6] = m1[6]-m2[6];
	temp[7] = m1[7]-m2[7];
	temp[8] = m1[8]-m2[8];
	return temp;
};

matrix operator*(const matrix m1, const matrix m2){
matrix temp;
	temp[0] = (m1[0]*m2[0]) + (m1[1]*m2[3]) + (m1[2]*m2[6]);
	temp[1] = (m1[0]*m2[3]) + (m1[1]*m2[4]) + (m1[2]*m2[7]);
	temp[2] = (m1[0]*m2[6]) + (m1[1]*m2[5]) + (m1[2]*m2[8]);
	temp[3] = (m1[3]*m2[0]) + (m1[4]*m2[3]) + (m1[5]*m2[6]);
	temp[4] = (m1[3]*m2[1]) + (m1[4]*m2[4]) + (m1[5]*m2[7]);
	temp[5] = (m1[3]*m2[2]) + (m1[4]*m2[5]) + (m1[5]*m2[8]);
	temp[6] = (m1[6]*m2[0]) + (m1[7]*m2[3]) + (m1[8]*m2[6]);
	temp[7] = (m1[6]*m2[1]) + (m1[7]*m2[4]) + (m1[8]*m2[7]);
	temp[8] = (m1[6]*m2[2]) + (m1[7]*m2[5]) + (m1[8]*m2[8]);
	return temp;
};
/*
ostream& operator<<(ostream& outp, const matrix& answer){
	outp << "[" << setw(2) << answer[0] << setw(2) << answer[1] << setw(2) << answer[2] << setw(2) << "]" << endl;
	outp << "[" << setw(2) << answer[3] << setw(2) << answer[4] << setw(2) << answer[5] << setw(2) << "]" << endl;
	outp << "[" << setw(2) << answer[6] << setw(2) << answer[7] << setw(2) << answer[8] << setw(2) << "]" << endl;
	outp << endl;
	return outp;
}

istream& operator>>(istream& inp, Fraction& stuff){
	


};*/

void display(int answer[]){
     
};


.h

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
#ifndef MATRIX_H
#define MATRIX_H

class matrix
{
            friend matrix operator+(const matrix, const matrix);
        	
            friend matrix operator-(const matrix,const matrix);
        	
            friend matrix operator*(const matrix,const matrix);
           
      public:
             
           
           //Constructor  
        	matrix();
        	//Gets
        	
            //Sets
            void setvalues(int[]);

        	
            
        	
     /*   	friend istream& operator>>();
        	
        	friend ostream& operator<<();
    	*/
        	
            void display(int[]);
           
            
      private:
	
	int matrix1;
	int temp;
	
};

#endif 


main.cpp

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
#include <cstdlib>
#include <iostream>
#include <fstream> 
#include <string>
#include <sstream>
#include "matrix.h"

using namespace std;

int main(){   
    //Variables/Instances
	int choice = 0;	
	int answer[9];
	int arraysize = 0;//Used to keep track of how big the array that were put the file into is
	int numberOfIntegers = 0;//number of total integers we pulled from file
	int globalcounter= 0;//to keep track of where we are at in the array
	int m1[9];
	int m2[9];
	string test = 0;//Just a test string (does not do anything)
	int size = 0;//
	
	matrix testm1;
	matrix testm2;
	matrix testanswer;
	
    //open file to find length
	ifstream myfile;
    myfile.open ("input.txt");
	
    //find size of array
	while (!myfile.eof()){ 
     getline(myfile, test);
     arraysize++;
    };
   //close file to restart 
   myfile.close();
   //declare array to hold strings coming from file
   string strfile[arraysize];
   //declare array to hold ints converted from file array
   int file[arraysize];
    //Open file
    myfile.open("input.txt");
	    
	//Pull from strings file
	while (!myfile.eof()){ 
     size++;     
     getline(myfile, strfile[size]);
     numberOfIntegers++;
   };
  
  //convert string array to int array
  
  for(int t = 0; t < size; t++){
       stringstream ssr(strfile[t]);
       ssr >> file[t];
       };
 
                            
  
  while (globalcounter != numberOfIntegers){
  
      //Pulls the decision
        choice = file[globalcounter];
        globalcounter++;
      
       //Seperate 9 integers from array to send to be turned into matrix 1
       for(int i = 0; i < 8; i++){           
                m1[i] = file[globalcounter];
                          globalcounter++;
              
              };        
        };
        //Seperate 9 integers from array to send to be turned into matrix 2
        for(int i = 0; i < 8; i++){           
                m2[i] = file[globalcounter];
                          globalcounter++;
              
              };        
        };
    	//Set Variables to Matrix
    	
        for(int i = 0; i < 8; i++){
             testm1[i] = m1[i];
             };
    	
        for(int i = 0; i < 8; i++){
             testm2[i] = m2[i];
             };
    	
    	//Decide what do do with 
        
        	switch(choice){
        		case 1: //addition
                        //fill Matrix
                                 
                        testanswer=testm1+testm2;
        			  
        			    break;
        			
        		case 2: //subtraction
        		        //fill Matrix
        		     
        		        testanswer=testm1-testm2;
        			    
        			    break;
        
        		case 3: //multiplication
        		        //fill Matrix
   			     
                        
        			    testanswer=testm1*testm2;
        		     	
        		      	break;
        
        		case 4: 
        			    myfile.close();
        		     	break;
        			
        	};
    	
     };
	
    system("PAUSE");
    return EXIT_SUCCESS;
};
Last edited on
matrix temp;
temp[0][0] = m1[0][0]+m2[0][0]; <<========your compilation error.

At this line specified above, you compiler looks for a definition of operator [] within the matrix class. Not finding one results in your error. Having an internal array representing a matrix doesn't mean that the semantics of that array will be automatically be translated to the class its declared in.

NB: In writing your overloaded definition of operator [], you might want to rethink how you're representing your matrix internally since it's multidimensional
I'm not sure how I would fix that. I don't want to have to overload the [] operator. Is there another variation of that statement that is syntacticly correct?
I don't even see an internal array in there anywhere...

Anyway, assuming you do end up with an internal array, the only way to really avoid overloading the array subscript operator is to expose the internal array. And in any case, overloaded the array subscript operator wouldn't let you have two of them in a row unless you created an intermediate type like "matrixRow" or something.
Topic archived. No new replies allowed.