Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;重载运算符=在模板中 所有我都遇到了C++模板操作符=< /p>的问题_C++_Templates_Operator Overloading - Fatal编程技术网

C++;重载运算符=在模板中 所有我都遇到了C++模板操作符=< /p>的问题

C++;重载运算符=在模板中 所有我都遇到了C++模板操作符=< /p>的问题,c++,templates,operator-overloading,C++,Templates,Operator Overloading,我想做的是: 我正在使用cuda进行一个图形算法项目,我们有几种不同的图形基准格式。另外,我也不完全确定我们最终会对图形的各个元素使用什么类型。 我的目标是拥有一个模板图类和许多其他类,每个类都知道如何加载特定的格式。除了graphCreator类从generate函数返回一个图形类型之外,一切似乎都正常工作。 这是我的密码: 图形运算符=: MatrixGraph<T>& operator=(MatrixGraph<T>& rhs)

我想做的是: 我正在使用cuda进行一个图形算法项目,我们有几种不同的图形基准格式。另外,我也不完全确定我们最终会对图形的各个元素使用什么类型。
我的目标是拥有一个模板图类和许多其他类,每个类都知道如何加载特定的格式。除了graphCreator类从generate函数返回一个图形类型之外,一切似乎都正常工作。 这是我的密码:

图形运算符=:

      MatrixGraph<T>& operator=(MatrixGraph<T>& rhs)
      {
         width = rhs.width;
         height = rhs.height;
         pGraph = rhs.pGraph;
         pitch = rhs.pitch;
         sizeOfGraph = rhs.sizeOfGraph;    
         rhs.Reset();
      }
MatrixGraph&operator=(MatrixGraph&rhs)
{
宽度=rhs.宽度;
高度=rhs高度;
pGraph=rhs.pGraph;
螺距=右螺距;
sizeOfGraph=rhs.sizeOfGraph;
rhs.Reset();
}
reset()调用删除对已分配内存的所有引用,这样rhs就不会释放这些引用。仅允许一个图形引用已分配的图形内存

图形副本构造函数:

   MatrixGraph(MatrixGraph<T>& graph)
   {
        (*this) = graph;
   }
MatrixGraph(MatrixGraph&graph)
{
(*此)=图形;
}
Graph Creator加载函数:

MatrixGraph<T> LoadDIMACSGraphFile(std::istream& dimacsFile)
{
char inputType;
std::string input;

GetNumberOfNodesAndEdges(dimacsFile, nodes, edges);

MatrixGraph<T> myNewMatrixGraph(nodes);

while(dimacsFile >> inputType)
{
  switch(inputType)
  {
    case 'e':
      int w,v;
      dimacsFile >> w >> v;
      myNewMatrixGraph[w - 1][v - 1] = 1;
      myNewMatrixGraph[v - 1][w - 1] = 1;
      break;

    default:
      std::getline(dimacsFile, input);
      break;
  }
}

  return myNewMatrixGraph;
}
MatrixGraph加载DIMACSGraphFile(std::istream&dimacsFile)
{
字符输入类型;
std::字符串输入;
GetNumberOfNodeAndedges(dimacsFile、节点、边);
MatrixGraph myNewMatrixGraph(节点);
while(dimacsFile>>输入类型)
{
开关(输入类型)
{
案例“e”:
int w,v;
dimacsFile>>w>>v;
myNewMatrixGraph[w-1][v-1]=1;
myNewMatrixGraph[v-1][w-1]=1;
打破
违约:
std::getline(dimacsFile,输入);
打破
}
}
返回myNewMatrixGraph;
}
最后,在main.cpp中,我尝试对其进行单元测试,我使用它:

DIMACSGraphCreator<short> creator;
myGraph = creator.LoadDIMACSGraphFile(instream);
DIMACSGraphCreator;
myGraph=creator.LoadDIMACSGraphFile(流内);
当我尝试编译时,出现以下错误:

main.cpp: In function 'int main(int, char**)':
main.cpp:31: error: no match for 'operator=' in 'myGraph = DIMACSGraphCreator<T>::LoadDIMACSGraphFile(std::istream&) [with T = short int](((std::istream&)(& instream.std::basic_ifstream<char, std::char_traits<char> >::<anonymous>)))'
MatrixGraph.h:103: note: candidates are: MatrixGraph<T>& MatrixGraph<T>::operator=(MatrixGraph<T>&) [with T = short int]
make: *** [matrixTest] Error 1
main.cpp:在函数“int main(int,char**)”中:
main.cpp:31:错误:myGraph=DIMACSGraphCreator::LoadDIMACSGraphFile(std::istream&)[带T=short int int]((std::istream&)(&instream.std::basic_ifstream:)”中的“operator=”不匹配
MatrixGraph.h:103:注意:候选项是:MatrixGraph&MatrixGraph::operator=(MatrixGraph&)[带T=short int int]
make:**[matrixTest]错误1

只是猜测,您的复制构造函数和赋值中是否碰巧缺少常量限定符?

只是猜测,您的复制构造函数和赋值中是否碰巧缺少常量限定符?

问题是您正在按值返回(正确),但试图将临时对象绑定到非常量引用(对于op=参数)。您不能这样做

解决方案是改变周围的情况,这可能会导致非惯用代码;使用类似auto_ptr_ref的构造,它以一种相当糟糕但封装的方式解决了这一问题;或者使用r值引用,它们正是为这种情况而设计的。然而,r值引用仅作为C++0x的一部分可用,并且您的编译器可能不支持支持他们吧

请确保在op=中也返回
*此
。在没有警告的情况下,编译器可能会在没有返回语句的情况下(违反标准)以静默方式接受该函数。(我不知道为什么。)

第一个解决方案的示例:

// move return value into output-parameter:
void LoadDIMACSGraphFile(std::istream& dimacsFile, MatrixGraph<T>& dest);

// ...

DIMACSGraphCreator<short> creator;
creator.LoadDIMACSGraphFile(instream, myGraph);
//将返回值移到输出参数中:
无效加载DIMACSGRAPHFILE(std::istream和dimacsFile、MatrixGraph和dest);
// ...
DIMACSGraphCreator;
LoadDIMACSGraphFile(instream,myGraph);

std::auto_ptr
位于stdlib中,使用一个名为auto_ptr_ref的特殊“holder”类来实现移动语义。

问题是您正在按值返回(正确),但试图将临时对象绑定到一个非常量引用(对于op=参数)。您不能这样做

解决方案是改变周围的情况,这可能会导致非惯用代码;使用类似auto_ptr_ref的构造,它以一种相当糟糕但封装的方式解决了这一问题;或者使用r值引用,它们正是为这种情况而设计的。然而,r值引用仅作为C++0x的一部分可用,并且您的编译器可能不支持支持他们吧

请确保在op=中也返回
*此
。在没有警告的情况下,编译器可能会在没有返回语句的情况下(违反标准)以静默方式接受该函数。(我不知道为什么。)

第一个解决方案的示例:

// move return value into output-parameter:
void LoadDIMACSGraphFile(std::istream& dimacsFile, MatrixGraph<T>& dest);

// ...

DIMACSGraphCreator<short> creator;
creator.LoadDIMACSGraphFile(instream, myGraph);
//将返回值移到输出参数中:
无效加载DIMACSGRAPHFILE(std::istream和dimacsFile、MatrixGraph和dest);
// ...
DIMACSGraphCreator;
LoadDIMACSGraphFile(instream,myGraph);

std::auto_ptr
在stdlib中,使用一个名为auto_ptr_ref的特殊“holder”类来实现移动语义。

您不显示
myGraph
的声明。我们需要它。另外,您似乎忽略了“模板”声明的一部分…您可以添加它吗?您不显示
myGraph
的声明。我们需要它。此外,您似乎忽略了声明的“模板”部分…您可以添加它吗?