求助啊TemplateError:Read-only variable is not assignable..
时间:2022-09-11 18:22:37
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/main.cpp:28:22: Read-only variable is not assignable
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/main.cpp:39:22: Read-only variable is not assignable
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/main.cpp:62:15: No viable overloaded '='
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/NumericArray.cpp
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/NumericArray.cpp:74:20: Reference to stack memory associated with local variable 'Temp' returned
//
// NumericArray.hpp
// Section4.2b.2
//
// Created by Cornelia on 10/16/16.
//
//
#ifndef NumericArray_hpp
#define NumericArray_hpp
#include "Array.hpp"
#include <stdio.h>
#include <iostream>
using namespace std;
namespace Corneliagao{
namespace CAD{
template <typename T>
class NumericArray: public Array<T>
{
private:
public:
//Default constructor
NumericArray();
//Constructor with a size argument
NumericArray(int size);
//Operator *
template <typename T>
NumericArray<T> NumericArray<T>::operator* (T factor)
{
NumericArray<T> Temp(Array<T>::Size());
for (int i = 0; i < Array<T>::Size(); i++)
{
Temp = factor*((*this).GetElement(i));
}
return Temp;
}
//Operator +
template <typename T>
NumericArray<T>& NumericArray<T>::operator + ( NumericArray<T> arrayt)
{
NumericArray<T> Temp(Array<T>::Size());
for(int i = 0; i < Array<T>::Size(); i++)
{
Temp = (*this).GetElement(i)+ arrayt.GetElement(i);
}
return Temp;
}
//Function Dotproduct
template<typename T>
T NumericArray<T>::DotProduct( NumericArray<T> & arr1)
{
T result;
for (int i = 0; i <Array<T>::Size(); i++)
result += NumericArray<T>::GetElement(i) * (arr1.GetElement(i));
return result;
}
}
}
#endif
//
// main.cpp
// Section4.2b.2
//
// Created by Cornelia on 10/16/16.
//
#include <iostream>
#include <cmath>
#include "Array.hpp"
#include "NumericArray.hpp"
//#include "Array.hpp"
//when put "#ifndef Array_cpp #include "Array.cpp" #endif" before #endif in header file, the main.cpp could still include "Array.hpp" instead of 'Array.cpp"
#include "iostream"
using namespace Corneliagao::CAD;
using namespace std;//Declaration to save "std" when use count/cin
//Assumption:type used as template argument must be numeric, it could do numeric operation, like plus, scale.
int main()
{
NumericArray<int> intArray1; //default constructor
NumericArray<int> intArray2(4); //initialize array
for (int i = 0; i < 3; i++)
{
intArray2[i] = i + 1;
cout << intArray2[i] << endl;
}
intArray1 = intArray2; //test assignment operator
for (int i = 0; i < 3; i++)
cout << intArray1[i] << endl;
#ifndef Array_cpp
#include "Array.cpp"
#endif/* Array_cpp */
//When just include header, main.cpp can't see the template definition of Array::Array() 0r Array::~Array. When include Array.cpp, it could know current transformation of Array<T>::Array() and Array<T>::~Array(). However, placing these code here is to put all of the template class code in the header file. Therefore, when include the header, all of the template code will be in one place, and main.cpp could identify them.
https://isocpp.org/wiki/faq/templates
在这个网站上搜到的,原本似乎要将模板.cpp include在main.cpp里面解决问题,但如果把上面三行添加在header,就include heade也可以。。。所以我就没合并,而且其他程序这么做也可以运行诶
刚刚试了一下全挪到header里,那几个错误还是存在诶。。郁闷。。。还有别的方法么
#3
你的行号对吗 ...
#4
哦哦,错误行号:main.cpp 26,37,60
numericarray.cpp 74
#5
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/NumericArray.cpp:74:20: Reference to stack memory associated with local variable 'Temp' returned
不能返回局部变量的引用。
=======================
Array 的 operator[] 是啥样的?
#6
你的行号对吗 ...
哦哦,错误行号:main.cpp 26,37,60
numericarray.cpp 74
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/NumericArray.cpp:74:20: Reference to stack memory associated with local variable 'Temp' returned
不能返回局部变量的引用。
=======================
Array 的 operator[] 是啥样的?
template <typename T>
const T& Array<T>::operator [] (int index) const
{
// Out of bound error code
if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
throw OutOfBoundsException(index);
}
else
{
return m_data[index];
}
}
#7
你的行号对吗 ...
哦哦,错误行号:main.cpp 26,37,60
numericarray.cpp 74
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/NumericArray.cpp:74:20: Reference to stack memory associated with local variable 'Temp' returned
不能返回局部变量的引用。
=======================
Array 的 operator[] 是啥样的?
template <typename T>
const T& Array<T>::operator [] (int index) const
{
// Out of bound error code
if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
throw OutOfBoundsException(index);
}
else
{
return m_data[index];
}
}
这里返回的是一个 const 引用,咋能给它赋值呢 ...
#8
你的行号对吗 ...
哦哦,错误行号:main.cpp 26,37,60
numericarray.cpp 74
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/NumericArray.cpp:74:20: Reference to stack memory associated with local variable 'Temp' returned
不能返回局部变量的引用。
=======================
Array 的 operator[] 是啥样的?
template <typename T>
const T& Array<T>::operator [] (int index) const
{
// Out of bound error code
if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
throw OutOfBoundsException(index);
}
else
{
return m_data[index];
}
}
这里返回的是一个 const 引用,咋能给它赋值呢 ...
啊,那怎么调试啊,自己弄不懂了
#1
我记得你发的上一个贴不就说过: 模板不要把声明和实现放在两个文件里
#2
#ifndef Array_cpp
#include "Array.cpp"
#endif/* Array_cpp */
//When just include header, main.cpp can't see the template definition of Array::Array() 0r Array::~Array. When include Array.cpp, it could know current transformation of Array<T>::Array() and Array<T>::~Array(). However, placing these code here is to put all of the template class code in the header file. Therefore, when include the header, all of the template code will be in one place, and main.cpp could identify them.
https://isocpp.org/wiki/faq/templates
在这个网站上搜到的,原本似乎要将模板.cpp include在main.cpp里面解决问题,但如果把上面三行添加在header,就include heade也可以。。。所以我就没合并,而且其他程序这么做也可以运行诶
刚刚试了一下全挪到header里,那几个错误还是存在诶。。郁闷。。。还有别的方法么
#3
你的行号对吗 ...
#4
你的行号对吗 ...
哦哦,错误行号:main.cpp 26,37,60
numericarray.cpp 74
#5
你的行号对吗 ...
哦哦,错误行号:main.cpp 26,37,60
numericarray.cpp 74
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/NumericArray.cpp:74:20: Reference to stack memory associated with local variable 'Temp' returned
不能返回局部变量的引用。
=======================
Array 的 operator[] 是啥样的?
#6
你的行号对吗 ...
哦哦,错误行号:main.cpp 26,37,60
numericarray.cpp 74
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/NumericArray.cpp:74:20: Reference to stack memory associated with local variable 'Temp' returned
不能返回局部变量的引用。
=======================
Array 的 operator[] 是啥样的?
template <typename T>
const T& Array<T>::operator [] (int index) const
{
// Out of bound error code
if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
throw OutOfBoundsException(index);
}
else
{
return m_data[index];
}
}
#7
你的行号对吗 ...
哦哦,错误行号:main.cpp 26,37,60
numericarray.cpp 74
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/NumericArray.cpp:74:20: Reference to stack memory associated with local variable 'Temp' returned
不能返回局部变量的引用。
=======================
Array 的 operator[] 是啥样的?
template <typename T>
const T& Array<T>::operator [] (int index) const
{
// Out of bound error code
if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
throw OutOfBoundsException(index);
}
else
{
return m_data[index];
}
}
这里返回的是一个 const 引用,咋能给它赋值呢 ...
#8
你的行号对吗 ...
哦哦,错误行号:main.cpp 26,37,60
numericarray.cpp 74
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/NumericArray.cpp:74:20: Reference to stack memory associated with local variable 'Temp' returned
不能返回局部变量的引用。
=======================
Array 的 operator[] 是啥样的?
template <typename T>
const T& Array<T>::operator [] (int index) const
{
// Out of bound error code
if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
throw OutOfBoundsException(index);
}
else
{
return m_data[index];
}
}