简体   繁体   English

尝试模拟向量时出现编译器错误C2106

[英]Compiler Error C2106 when trying to simulate a vector

I'm trying to write a fake vector for my class assignment, and I currently get an error in the member function pushBack. 我正在尝试为我的类分配编写伪造的向量,并且当前在成员函数pushBack中出现错误。 The compiler doesn't seem to like incrementing the SIZE variable which holds the number of elements in the "vector". 编译器似乎不喜欢增加保存“向量”中元素数量的SIZE变量。 Is there something I might need to fix? 我可能需要修复一些东西吗? Your assistance would be highly appreciated for helping me with this, and any other problems you might happen to find. 感谢您为我提供的帮助以及您可能碰到的任何其他问题的帮助。

/*
Write a simple program that simulates the behavior of vectors
-You should be able to add and remove elements to the vector
-You should be able to access an element directly.
-The vector should be able to hold any data type.
*/


#include <stdio.h>

template <class T, int SIZE>
class Vector
{
 #pragma region constructors&destructors
 private:
 T vec[SIZE];

 public:

 Vector()
 {}
 ~Vector()
 {}
 #pragma endregion

 template <class T/*, int SIZE*/>
 void defineVec(T var)
 {
  for(int i=0; i<SIZE; i++)
  {
   vec[i] = var;
  }
  //printf("All elements of the vector have been defined with %", var)
  //What should I do when trying to print a data type or variable 
                //of an unspecified one along with the '%'?
 }

 template <class T/*, int SIZE*/>
 void pushBack(T var)
 {
  SIZE ++; //C1205
  vec[SIZE - 1] = var;
 }

 template <class T/*, int SIZE*/>
 void popBack()
 {
  vec[SIZE - 1] = NULL;
  SIZE--;
 }

 //template <class T/*, int SIZE*/>
 void showElements()
 {
  for(int i=0; i<SIZE; i++)
  {
   printf("%d",vec[i]);
   printf("\n");
  }
 }
};

int main()
{
 Vector <int, 5> myints;
 myints.pushBack(6);
 myints.showElements();
 return 0;
}

You're passing SIZE as a template parameter. 您正在传递SIZE作为模板参数。 Inside the definition of a template, a non-type template parameter is basically a constant -- ie, you can't modify it. 在模板的定义内,非类型模板参数基本上是一个常量-即,您无法对其进行修改。

You'll need to define a separate variable to keep track of how much of the storage in your vector-alike is currently being used. 您需要定义一个单独的变量,以跟踪当前类似载体中有多少存储空间正在使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 C2106:“=”:尝试从函数更改值时,左操作数必须是左值 - error C2106: '=': left operand must be l-value when trying to change value from function 错误C2106:'=':左操作数必须是l值 - error C2106: '=' : left operand must be l-value 错误C2106:&#39;=&#39;:左操作数必须为左值c ++ - error C2106: '=' : left operand must be l-value c++ C++ 错误 C2106:“=”:左操作数必须是左值 - C++ error C2106: '=' : left operand must be l-value 错误C2106:&#39;=&#39;:通过C ++中的动态编程,斐波那契数列中的左操作数必须为l值 - error C2106: '=' : left operand must be l-value in Fibonacci sequence by dynamic programming in C++ 错误 C2106:“=”:左操作数必须是带有 `!=` C++ 的左值 - error C2106: '=' : left operand must be l-value with `!=` C++ 标准::复杂<float>错误 C2106:“=”:左操作数必须是左值</float> - std::complex<float> with error C2106: '=': left operand must be l-value 尝试打印出向量内容时出现编译器错误 - Getting compiler error when trying to print out contents of vector C ++编译器错误(尝试创建向量的静态向量) - C++ Compiler Error (trying to create a static vector of vectors) 尝试 output 向量时出现第 30 行的编译器错误 - Compiler error at line 30 when trying to output vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM