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