I haven't used C++ Templates and I am getting multiple syntax errors

Pages: 12
I am using C++ Visual Studio Community 2015. I am using the template with a class that has a std::array. I tried moving the function definitions after main, but I get more errors. I would greatly appreciate any help on this. Thanks.

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <array>
#pragma warning(disable: 4522)

#define DATAROW_LENGTH 4
using namespace std;

template<size_t T_width>

class DataContainer : public array<double, T_width> //<datatype,array size> double is datatype inside array & T_width is
{
std::array<double, T_width> arr;
public:

	DataContainer();
	DataContainer operator+(const DataContainer &);
	DataContaineroperator+=(const DataContainer &);
	DataContainer Powr( int power);
	DataContainer Sqrt();
	DataContainer operator-(const DataContainer &);
	DataContainer operator-=(const DataContainer &);
	DataContainer operator/( int value);
	DataContainer operator=(const DataContainer&);
	virtual ~DataContainer();


	template<size_t T_width>
	DataContainer()
	{
		arr = new double[T_width];
	}
	template<size_t T_width>
	DataContainer<T_width>::operator=(const DataContainer&)//LINE 37
	{
		if (this == &rhs) {
			return *this;
		}

		delete[] arr;
		arr = new double[T_width];

		for (int i = 0; i < T_width; i++)
		{
			arr[i] = rhs[i];
		}
		return *this;
	}
       
	template<size_t T_width>
	DataContainer <T_width>::operator+(const DataContainer &)
	{                                                     //LINE 54

		for (int i = 0; i < a; i++)
		{
 			arr[i] = arr[i] + rhs;
		}

		return *this;
	}
        
	template<size_t T_width>
	DataContainer<T_width>::operator-(const DataContainer &)//LINE 64
	{
		for (int i = 0; i < T_width; i++)
		{
 			arr[i] = arr[i] - rhs[i];
		}
		return *this;
	}
	template<size_t T_width>
	DataContainer<T_width>::operator-=(const DataContainer &)// LINE 74
	{                                                        
		for (int i = 0; i < T_width; i++)
		{
			  arr[i] -= rhs[i];
		}
		return *this;
	}
        template<size_t T_width>
        DataContainer<T_width>::operator+=(const DataContainer &)
	{
		for (int i = 0; i < T_width; i++)
		{
			arr[i] += rhs[i];
		}
		return *this;
	}
	template<size_t T_width>
	DataContainer<T_width>::operator/(int value)        // LINE91
	{

		for (int i = 0; i < T_width; i++)
		{
			arr[i] = arr[i] / value;
		}
		return *this;
	}
	
	template<size_t T_width>
	DataContainer<T_width>::Powr(int power)
	{                                             //LINE 107
		DataContainer d;
		for (int = 0; i < T_width; i++)
		{
			arr[i] = pow(rhs[i], power);
		}
		return *this;
	}
	template<size_t T_width>
	DataContainer<T_width>::Sqrt() //LINE 115
	{
               for (int = 0; i < T_width; i++)
		{
  			arr[i] = sqrt(rhs[i]);
		}
		return *this;
	}
        template<size_t T_width>
	virtual DataContainer<T_width>:: ~DataContainer() // LINE 126
	{
		delete[] arr;
	
	}
};


int main(int argc, char** argv)
{	
	typedef DataContainer< DATAROW_LENGTH > DataRow;//typedef is an alias name
	DataRow d1, d2, d3;	
	
	d1.fill(10); 
	d2.fill(20);
	d3 = d1 + d2;		// Elementwise addition
	d3 = d1 - d2;		// Elementwise subtraction
	d3 = d1 / 5;		// Divide all elements by a constant
	d3 += d1; 		// Compound assignment
	d3 -= d2;
	d3 = d1.Powr(2); //Raise all elements to a constant power(d1 unchanged)
	d3 = d2.Sqrt(); 	// Square root of element (d2 unchanged)
	
	return 0;
}





8 ERRORS/ 4 WARNINGS:

Severity Code Description Project File Line Suppression State
Error C2061 syntax error: identifier '=' ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 37

Error C2334 unexpected token(s) preceding '{'; skipping apparent function body ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 54

Error C2061 syntax error: identifier '-' ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 64

Warning C4346 '-': dependent name is not a type ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 64

Warning C4346 '=': dependent name is not a type ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 37

Error C2334 unexpected token(s) preceding '{'; skipping apparent function body ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 74

Warning C4346 '/': dependent name is not a type ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 91

Error C2061 syntax error: identifier '/' ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 91

Error C2334 unexpected token(s) preceding '{'; skipping apparent function body ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 107

Warning C4346 'Sqrt': dependent name is not a type ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 116

Error C2061 syntax error: identifier 'Sqrt' ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 116

Error C2334 unexpected token(s) preceding '{'; skipping apparent function body ConsoleApplication4 c:\users\vonic\documents\visual studio 2015\projects\consoleapplication4\consoleapplication4\main.cpp 126

Last edited on
There is a lot wrong with this. I mean a lot. The problems aren't just templates. Lots of the functions just make no sense or are missing things.

Like your Sqrt() function. It tries to use rhs, but rhs doesn't exist. That's just one example. There's others like that, and it looks like at some point all your functions were outside the class declation, but now you've just copied them in, so you've got all the collisions of declarations and then later on definitions of the same inside the class.

How did you write so much without it ever compiling?

operator+ uses "a". What's a?

for (int = 0; i < T_width; i++) - you've missed out the declaration of i.

The problems in this code are not so much templates as just a whole lot of errors in writing code and in making a class.

arr = new double[T_width];
arr is a std::array, that new returns a pointer. This just makes no sense. It feels like you're mixing up std::array and C style arrays. They're different things. The point of std::array is so you DON'T have to mess around with new.


I would recommend abandoning templates. Write the code without templates. Make it work for a single case. A single fixed size. THEN, you can turn it into a template.

The class itself even makes no sense. It's inheriting from array<double, T_width> , but it also contains one of those? Makes no sense. If it IS one of those, why contain another one? Or if it's just going to be a wrapper around one of those, why inherit from one?
Last edited on
The arguments that take DataContainer should be DataContainer<T_Width>.
> DataContainer<T_width>::operator=(const DataContainer&)//LINE 37
Yeah, and that's where you should have stopped, thought about what you're doing and FIXED the issue.

Not charge ahead in your own magnificence believing your awesome program would compile and work first time.

What if your approach is completely unfixable?
You've just wasted a lot of effort that has to be re-written.

Slow down, compile and test OFTEN.
Magnificence? Wow that's some assumption.
Salem,

how about some help instead of the snide comments.
Your hole - you dig yourself out.

Best thing to do with the code right now - delete it and start again.

Writing pages of code and dumping it on a forum when you get pages of errors is NOT a long term strategy. You need to figure out how to code within your capabilities.

Clearly, you're not there yet.

Your idea of "help" is for someone else to fix the mess you created.

This is not a long term strategy of mine and of course I don't know how to do this, that is why I'm asking for help. That is the point of this forum.

Clearly, I'm not there yet or I would not be asking for help.

Please don't respond to my posts if you don't like it.
Well, you have been told that your code contains lots and lots of problems that are nothing to do with templates.

Have you fixed all those problems? All the ones that are nothing to do with templates? If you came back with all those basics mistakes fixed, so that you have a non-template class that worked, then adding the template would be easy.
Last edited on
 
class DataContainer : public array<double, T_width>


 
std::array<double, T_width> arr;


Why?
The code is working now. I followed the advice of the first reply to do a program with the std::array<> and I put the function definitions in the class. Anyways, compiles and works as I expect.
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

#pragma
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <array>
#pragma warning(disable: 4522)

#define DATAROW_LENGTH 4
using namespace std;

template<size_t T_width>

class DataContainer : public array<double, T_width> //<datatype,array size> double is datatype inside array & T_width is
{

	
public:
	std::array<double, T_width> arr;

	DataContainer() {
				
		for (int i = 0; i < T_width; i++)
		{
			arr[i] = 0;
		}
	}
	DataContainer(const DataContainer& otherObject)
	{
		for (int i = 0; i < T_width; i++)
		{
			arr[i] = otherObject.arr[i];
		}
	}
	friend DataContainer operator+(DataContainer& rhs,const DataContainer& rhs2)
	{
		DataContainer temp;
		for (int i = 0; i <T_width; i++)
		{
			temp.arr[i] = rhs.arr[i] + rhs2.arr[i];
		}

		return temp;
	}
	
	void fill(double input)
	{
		arr.fill(input);
	}
	DataContainer Powr( int power)
	{
		DataContainer d;
		for (int i = 0; i < T_width; i++)
		{
			arr[i] = pow(arr[i], power);
		}
		return *this;
	}
	DataContainer Squrt()
	{
		for (int i = 0; i < T_width; i++)
		{
			arr[i] = sqrt(arr[i]);
		}
		return *this;
	}
	friend DataContainer operator-(const DataContainer& rhs,const DataContainer& rhs2)
	{
		DataContainer temp;
		for (int i = 0; i < T_width; i++)
		{
			temp.arr[i] = rhs.arr[i] - rhs2.arr[i];
		}
		return temp;
	}
	DataContainer operator-=(const DataContainer& rhs)
	{
		for (int i = 0; i < T_width; i++)
		{
			this->arr[i] = this->arr[i] - rhs.arr[i];
		}
		return *this;
	}
	friend DataContainer operator/( const DataContainer& rhs,int value)
	{
		DataContainer temp;
		for (int i = 0; i < T_width; i++)
		{
			temp.arr[i] = rhs.arr[i] / value;
		}
		return temp;
	}
	DataContainer operator=(const DataContainer& rhs)
	{
		if (this == &rhs) {
			return *this;
		}

		
		for (int i = 0; i < T_width; i++)
		{
			arr[i] = rhs.arr[i];
		}
		return *this;
	}
	DataContainer& operator+=( DataContainer& rhs)
	{
		DataContainer temp;
		for (int i = 0; i < T_width; i++)
		{
			this->arr[i] = this->arr[i] + rhs.arr[i] ;
		}
		return *this;
	}
	void printArray()const
	{
		for (unsigned int i = 0; i < arr.size(); i++)
		{
			cout << arr[i] << endl;
		}
	}	
};

int main(int argc, char** argv)
{	
	typedef DataContainer< DATAROW_LENGTH > DataRow;//typedef is an alias name
	DataRow d1, d2, d3;	
	
	d1.fill(10); 
	cout << "AFTER d1 FILL" << endl;
	d1.printArray();

	d2.fill(20);
	cout << "AFTER d2 FILL" << endl;
	d2.printArray();

	d3 = d1 + d2;		// Elementwise addition
	cout << "======D3 AFTER d3 = d1+d2" << endl;
	d3.printArray();
	
	d3 = d1 - d2;		// Elementwise subtraction
	cout << "===========D3 AFTER d3 = d1-d2" << endl;
	d3.printArray();	

	d3 = d1 / 5;		// Divide all elements by a constant
	cout << "=======D3 AFTER d3 = d1/5" << endl;
	d3.printArray();
	
	cout << " =============D1========" << endl;
	d1.printArray();
	
	d3 += d1; 		// Compound assignment
	cout << "===============D3 AFTER d3 += d1" << endl;
	d3.printArray();

	d3 -= d2;
	cout << "===============D3 AFTER d3 -= d2" << endl;
	d3.printArray();

	d3 = d1.Powr(2); 	// Raise all elements to a constant power(d1 unchanged)
	cout << "===============D3 AFTER d3 = d1.pow(2)" << endl;
	d3.printArray();

	d3 = d2.Squrt(); 	// Square root of element (d2 unchanged)
	cout << "==============D3 AFTER d3 = d2.sqrt" << endl;
	d3.printArray();
	
	return 0;
}
Line 2.
#pragma
What? Is that a question or comment?
You have a pragma without anything following it. It shouldn't be there.
You shouldn't be publicly inheriting from std::array.
Are you referring to line 15?
That part of the code was given to me by someone else for me to solve and so was the code in main. I only defined all the functions to make the code work in main. What is the problem with doing that?
std::array<double, T_width> arr;

Did you add that? That line makes the inheritance completely pointless and nonsensical.

If you have that line, you can change line 15 to:
class DataContainer

You don't need inheritance at all in this.
Last edited on
Yes. Where should I have named the array? Or what is a better solution?
Inheritance not needed.

Line 15:
class DataContainer

Pages: 12