English 中文(简体)
使用 自己 + () 函数 (“ std:: plus is not a 函数 ”) 时奇怪的 g+++ 编译器错误)
原标题:Strange g++ compiler error when using my own plus() function ("std::plus is not a function")

g++ 显示一个奇怪的编译错误 。

上面写着“ std::plus is not a 函数 ” for the follow code, 尽管我并不包括 lt; 功能 & gt; 和不使用发生错误的

这是代码:

#include <iostream>

template<class T1, class T2, class T3>
struct MyStruct {
  T1 t1; T2 t2; T3 t3;
  MyStruct() {}
  MyStruct(T1 const& t1_, T2 const& t2_, T3 const& t3_)
    : t1(t1_), t2(t2_), t3(t3_) {}
};

template<class T1, class T2, class T3>
MyStruct<T1, T2, T3> plus(MyStruct<T1, T2, T3> const& x,
              MyStruct<T1, T2, T3> const& y) {
  // ...
}

int main() {
  typedef MyStruct<int, double, std::string> Struct;
  Struct x(2, 5.6, "bar");
  Struct y(6, 4.1, "foo");
  Struct result = plus(x, y);
}

这是全部错误( 略微重写) :

/usr/include/c++/4.2.1/bits/stl_function.h: In function  int main() :
/usr/include/c++/4.2.1/bits/stl_function.h:134:
    error:  template<class _Tp> struct std::plus  is not a function,
plus3.cc:13: error:
   conflict with  template<class T1, class T2, class T3> 
     MyStruct<T1, T2, T3> plus(const MyStruct<T1, T2, T3>&, 
       const MyStruct<T1, T2, T3>&) 
plus3.cc:21: error:   in call to  plus 

有谁知道这是为什么, 以及如何避免错误? 我真的想调用该函数 plus

我的 plus 函数没有3个模板参数时,就不会发生错误。 查看 std:: plus 的定义后, 3个模板参数是有道理的 :

  template <class _Tp>
    struct plus : public binary_function<_Tp, _Tp, _Tp>

但还是很奇怪, 因为std::+ 当时甚至不知道。

<强 > UPATE:

针对某些答案, 我会粘贴略微修改的代码, 并给出错误 。 我的 < code> plus 函数在 < code> namespace foo 中在此, 它被调用同一命名空间, 所以不需要使用 < code> foo: 来限定它 : < / code> :

#include <string>

namespace foo {

template<class T1, class T2, class T3>
struct MyStruct {
  T1 t1; T2 t2; T3 t3;
  MyStruct() {}
  MyStruct(T1 const& t1_, T2 const& t2_, T3 const& t3_)
      : t1(t1_), t2(t2_), t3(t3_) {}
};

template<class T1, class T2, class T3>
MyStruct<T1, T2, T3> plus(MyStruct<T1, T2, T3> const& x,
                          MyStruct<T1, T2, T3> const& y) {
  // ...                                                                                                                        
}

template<class T1, class T2, class T3>
MyStruct<T1, T2, T3> bar(MyStruct<T1, T2, T3> const& x,
                         MyStruct<T1, T2, T3> const& y) {
  return plus(x, y);
}

} // end foo                                                                                                                    

int main() {
  typedef foo::MyStruct<int, double, std::string> Struct;
  Struct x(2, 5.6, "bar");
  Struct y(6, 4.1, "foo");
  Struct result = foo::bar(x, y);
}
问题回答

之所以如此,是因为Std:+不是函数。

您的函数加在全域命名空间中, 所以您需要做

Struct result = ::plus(x, y);

之所以试图使用std:+ 是因为您在“加加”的版本中并不清楚, 所以编译者使用

如果您将函数放入不同的命名空间, 您将不会获得此错误。 这就是命名空间的目的。 您可以在不同的命名空间里有相同的符号/ 签名, 而不引起碰撞 。

尝试指定您加上 () 的命名空间

Struct result = ::plus(x, y);

这对 gcc 有所帮助, 并且不会使您将您的附加( ) 放在不同的命名空间 。

洛基·阿斯塔里告诉其他人





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?