简体   繁体   English

c ++错误:在没有对象参数的情况下调用非静态成员函数

[英]c++ error: call to non-static member function without an object argument

I inherited some code that uses the class adapter pattern and I want to convert it to use the object adapter pattern. 我继承了一些使用类适配器模式的代码,我想将其转换为使用对象适配器模式。

The custom class string is adapting std::string , and it's in the MyString namespace. 自定义类string正在调整std::string ,它位于MyString命名空间中。
Here's a snippet of how the code looks before I alter it. 这是代码在我改变代码之前的样子片段。

// mystring.h
namespace MyString
{

// StringInterface is the new (abstract) interface that the client will use.
// Inheriting the implementation of std::string to build on top of it.
class string : public StringInterface, private std::string
{
  ...
};

}

// mystring.cpp
namespace MyString
{

...

string& MyString::string::operator=(const string& s) // copy assignment operator
{
  if (this != &s) std::string::operator=(s);
  return *this;
}

...

}

Once I remove the private inheritance of std::string (which I do because--correct me if I'm wrong--the object adapter pattern uses composition and not inheritance of the implementation), the statement std::string::operator=(s); 一旦我删除了std::string的私有继承(我这样做是因为 - 如果我错了就纠正我 - 对象适配器模式使用组合而不是实现的继承),语句std::string::operator=(s); causes the error " call to non-static member function without an object argument ". 导致错误“ 调用非静态成员函数而没有对象参数 ”。

So I'm not really sure how to accomplish this. 所以我不确定如何实现这一目标。 It's my first time dealing with the adapter pattern (and C++ isn't my strongest language); 这是我第一次处理适配器模式(而C ++不是我最强的语言); maybe I'm overlooking something simple. 也许我忽略了一些简单的事情。

So assuming you have made the std::string a member of your class 因此,假设您已将std::string作为您的类的成员

class string : public StringInterface
{
   ...
   std::string m_str;
   ...
};

you should then modify all your operations on the once "inherited" (but it's privately... well) std::string to your now member std::string , which in my example, is m_str . 然后,您应该将曾经“继承”(但它是私有的......) std::string所有操作修改为现在的成员std::string ,在我的示例中,它是m_str For example, instead of doing std::string::size() , you should do m_str.size() . 例如,您应该执行m_str.size() ,而不是执行std::string::size() m_str.size()

For your operator=() , you should then do it this way: 对于你的operator=() ,你应该这样做:

string& MyString::string::operator=(const string& s) // copy assignment operator
{
  if (this == &s) return *this;  // So you'll only have to do this test once

  m_str = s.m_str; // instead of std::string::operator=(s);
  // ...

  return *this;
}

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

相关问题 C++ 线程“调用不带对象参数的非静态成员函数” - C++ Thread "Call to non-static member function without an object argument" 调用不带对象参数的非静态成员函数,C ++节点 - call to non-static member function without an object argument, C++ nodes 在没有object参数的情况下调用非静态成员函数 - Call to non-static member function without an object argument 调用非静态成员函数而没有对象参数编译器错误 - Call to non-static member function without an object argument compiler error 当调用没有对象参数的非静态成员函数时发生错误 - error occurs as call to non-static member function without an object argument 非法调用非静态成员函数(C ++)? - Illegal call to non-static member function (C++)? C ++错误C2352:'CSchedulerDlg :: Select':非法调用非静态成员函数 - C++ error C2352: 'CSchedulerDlg::Select' : illegal call of non-static member function 错误:非静态成员函数 C++ 的无效使用 - error: invalid use of non-static member function C++ C ++:在没有对象实例的情况下调用非静态成员函数 - C++: calling non-static member function without instance of object 没有构造函数的类中的C ++非静态const成员 - C++ non-static const member in class without a constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM