【问题标题】:How to use a method on a class object if method not defined in class c++如果方法未在类 c++ 中定义,如何在类对象上使用方法
【发布时间】:2016-08-14 02:58:13
【问题描述】:

我有课Pstring

class Pstring
{
private:
   string palindrome;



public:
   Pstring() { palindrome = ""; }
   Pstring(string pal) { setString(pal); }
   void setString(string pal) { palindrome = pal; }
   string getPal() const { return palindrome; }


};

我的主要方法Pstring palindrome(palin)中的一个对象由

定义
   string palin = "";

   cout << "Enter a palindrome:\n";
   getline(cin, palin);

   Pstring palindrome(palin);

和当前的测试方法bool isPalindrome(string pal)定义为

bool isPalindrome(string pal)
{
   bool flag; 

   cout << "Do I have access to this?";
   cout << pal; 

   //code goes here to check for palindrome, return bool

}

我想让我的 Pstring 类对象回文在 main 中使用方法 isPalindrome,但是当我尝试使用 palindrome.isPalinedrome(palin); 调用该方法时,它似乎无法访问该方法。

我可以做些什么来允许类外的方法被 main 中的类对象使用?

【问题讨论】:

  • 声明并定义它,例如。你不能使用不存在的东西。
  • @RemyLebeau,显然......我在问有没有办法。
  • @skypjack,什么是声明但未定义的?如果答案很明显,我深表歉意。
  • 一个类、一个结构、一个函数等等,都必须声明和定义。举个简单的例子,struct S; 是一个有效的语句,但这里没有 S 的定义,对吧?

标签: c++ class c++11 methods parameters


【解决方案1】:

您没有在Pstring 类中定义isPalinedrome() 方法,因此您不能在主代码中将其称为palindrome.isPalinedrome()

不要让Pstring尝试在你的主代码中调用一个函数,你应该将回文逻辑移动到Pstring,然后主代码可以在需要时询问Pstring

试试这个:

class Pstring
{
private:
   string value;

public:
   Pstring() { }
   Pstring(const string &s) { setString(s); }
   void setString(const string &s) { value = s; }
   string getString() const { return value; }

   // add this...
   bool isPalindrome() const {
      //code goes here to check value for palindrome, return bool
   }
};

那么你的主代码可以这样做:

bool isPalindrome(const string &value)
{
   Pstring palindrome(value);
   return palindrome.isPalindrome();

   // or simply:
   // return Pstring(value).isPalindrome();
}

int main()
{
   string palin;

   cout << "Enter a palindrome:\n";
   getline(cin, palin);

   if (isPalindrome(palin)) {
      // do something ...
   } else {
      // do something else...
   }

   return 0;
}

或者这个:

int main()
{
   string palin;

   cout << "Enter a palindrome:\n";
   getline(cin, palin);

   Pstring palindrome(palin);
   if (palindrome.isPalindrome()) {
      // do something ...
   } else {
      // do something else...
   }

   return 0;
}

【讨论】:

    【解决方案2】:

    您应该将您的测试方法添加到类中:

    class Pstring
    {
    private:
       string palindrome;
    
    public:
       // you don't need to initialize palindrome = "" (it's initialized by default)
       Pstring() {}
    
       // always pass strings as const reference unless you have
       // special reason to do it another way...
       Pstring(const string&  pal) { setString(pal); }
       void setString(const string& pal) { palindrome = pal; }
       string getPal() const { return palindrome; }
    
       bool isPalindrome() const // you don't have to pass string
       {
          bool flag; 
    
          cout << "Do I have access to this?";
          cout << palindrome; // please note this 
    
         //code goes here to check for palindrome, return bool
    
       }
    
    };
    

    另外请注意错字: palindrome.isPalinedrome(palin);

    【讨论】:

      【解决方案3】:

      你可以使用函数指针来实现它:

      声明类:

      class Pstring{
      private:
          string palindrome;
      public:
          Pstring() { palindrome = ""; }
          Pstring(string pal) { setString(pal); }
          void setString(string pal) { palindrome = pal; }
          string getPal() const { return palindrome; }
          //add a function pointer member:
          bool(*isPalindrome) (string);
      };
      

      然后定义函数:

       bool isPalindrome(string pal)
      {
          bool flag;
          cout << "Do I have access to this?";
          cout << pal;
      
          //code goes here to check for palindrome, return bool
          return true;
      }
      

      现在您可以在 main 函数中编写代码了:

      string palin = "";
      
      cout << "Enter a palindrome:\n";
      getline(cin, palin);
      
      Pstring palindrome(palin);
      palindrome.isPalindrome = isPalindrome;//bind function
      

      你现在可以通过对象来使用函数了:

      palindrome.isPalindrome(palin);
      

      【讨论】:

        【解决方案4】:

        对不起。创建这个类有什么意义?您唯一需要的是辅助函数 isPalindrome(const std::string&)。如果您需要某种范围保护,将其放入命名空间可能会更好

        对不起,我不得不说你只是让事情变得复杂。

        C++ 不是 java。如果你不使用它,你不应该为此付费。

        【讨论】:

        • 我是一名学生,只是想学习 c++ 的进出。我不是专业人士。感谢您的反馈。
        • 我也喜欢保持 main 尽可能干净。我更容易阅读我的代码。
        • 对不起,伙计。昨天心情不太好。请忽略我的态度。
        • 所有设置队友 :) 非常感谢。
        猜你喜欢
        • 1970-01-01
        • 2019-04-15
        • 2019-03-08
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-22
        • 1970-01-01
        相关资源
        最近更新 更多