Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm sorry for my bad English, but it isn't my language.

I've done an exercise in C++11 for University:
This is a program for understanding Templates:
C++
#include <iostream>
#include <cstdlib>
#include <ctime>

class matrix{
    public:
    matrix(int riga, int colonna){
        this->riga=riga;
        this->colonna=colonna;
    }
    matrix(){
        this->riga=0;
        this->colonna=0;
    }

    template <typename T> T crea(){
        auto a = new<T>*(riga);

        for(int i=0; i!=riga; ++i){
            a[i]=new<T>(colonna);
        }
    }

    private:
        int riga, colonna;
}
using namespace std;

int main(){
    auto v=crea<int>(5,10);
    srand(time(nullptr));


    for(int i=0, j=0; i!=5, j!=10; ++i, ++j){
        v[i][j]=rand()%100+1;
    }

    for(int i=0, j=0; i!=5, j!=10; ++i, ++j){
        cout<<v[i][j]<<endl;
    }
return 0;
}


Thanks in advance.

[Edit - Added from comments]
This is compiler result:
m.cpp: In member function ‘T matrix::crea()’:
m.cpp:17:15: error: expected type-specifier before ‘<’ token
   auto a = new<t>*(riga);
               ^
m.cpp:17:17: error: expected primary-expression before ‘>’ token
   auto a = new<t>*(riga);
                 ^
m.cpp:17:24: error: invalid type argument of unary ‘*’ (have ‘int’)
   auto a = new<t>*(riga);
                        ^
m.cpp:20:12: error: expected type-specifier before ‘<’ token
    a[i]=new<t>(colonna);
            ^
m.cpp:20:14: error: expected primary-expression before ‘>’ token
    a[i]=new<t>(colonna);
              ^
m.cpp: In function ‘int main()’:
m.cpp:31:9: error: ‘crea’ was not declared in this scope
  auto v=crea<int>(5,10);
         ^
m.cpp:31:14: error: expected primary-expression before ‘int’
  auto v=crea<int>(5,10);

[/Edit]

Update 2:
i've modified the code with this:
C++
#include <iostream>
#include <cstdlib>
#include <ctime>

class matrix{
	public:
	matrix(){
		this->riga=0;
		this->colonna=0;
	}
	~matrix(){
	}
	template <typename T> T crea(){
		auto a = new T*[riga];

		for(int i=0; i!=riga; ++i){
			a[i]=new T[colonna];
		}
		return a;
	}
	
	private:
		int riga, colonna;
};

using namespace std;

int main(){
	matrix c;
	auto v=c.crea<int>();
	srand(time(nullptr));

	for(auto i=0, j=0; i!=5, j!=10; ++i, ++j){
		v[i][j]=rand()%100+1;
	}

	for(auto i=0, j=0; i!=5, j!=10; ++i, ++j){
		cout<<v[i][j]<<endl;
	}
return 0;
}


and this is compiler result:
C++
m.cpp: In function ‘int main()’:
m.cpp:34:6: error: invalid types ‘int[int]’ for array subscript
   v[i][j]=rand()%100+1;
      ^
m.cpp:38:12: error: invalid types ‘int[int]’ for array subscript
   cout<<v[i][j]<<endl;
            ^
m.cpp: In instantiation of ‘T matrix::crea() [with T = int]’:
m.cpp:30:21:   required from here
m.cpp:19:10: error: invalid conversion from ‘int**’ to ‘int’ [-fpermissive]
   return a;
          ^
Posted
Updated 31-Oct-15 4:49am
v7
Comments
Afzaal Ahmad Zeeshan 31-Oct-15 8:22am    
Ok, but what is the problem? You should also write the problem you are having so that we can help you to solve it.
Garth J Lancaster 31-Oct-15 8:44am    
mi dispiace, mi parlo una piu d'italiano !

In addition to Afzaal's comment, please dont post in multiple forums (unless you got the forum so wrong someone tells you to repost/cross post somewhere else)
OriginalGriff 31-Oct-15 8:51am    
And?
What is the question?
Bear in mind that we can;t see your screen, access your HDD, or read your mind - and we have no idea what this is supposed to do, much less what it does that you didn't expect, or doesn't do that you did! :laugh:

So tell us!
Use the "Improve question" widget to edit your question and provide better information.
Stefano Lodico 31-Oct-15 9:16am    
This is compiler result:

m.cpp: In member function ‘T matrix::crea()’:
m.cpp:17:15: error: expected type-specifier before ‘<’ token
auto a = new<t>*(riga);
^
m.cpp:17:17: error: expected primary-expression before ‘>’ token
auto a = new<t>*(riga);
^
m.cpp:17:24: error: invalid type argument of unary ‘*’ (have ‘int’)
auto a = new<t>*(riga);
^
m.cpp:20:12: error: expected type-specifier before ‘<’ token
a[i]=new<t>(colonna);
^
m.cpp:20:14: error: expected primary-expression before ‘>’ token
a[i]=new<t>(colonna);
^
m.cpp: In function ‘int main()’:
m.cpp:31:9: error: ‘crea’ was not declared in this scope
auto v=crea<int>(5,10);
^
m.cpp:31:14: error: expected primary-expression before ‘int’
auto v=crea<int>(5,10);
phil.o 31-Oct-15 9:29am    
Please, do not post such long texts in comments; this is part of your question, so you should have pushed the "Improve question" button and put these informations there. I did it for you. Next time, please take care.

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