main 中的运算符重载和函数调用
Operator Overloading and function calling in main
我知道
有两个问题:
我不知道如何正确重载运算符(在这种情况下为括号)。我希望运算符重载调用私有成员函数"isprime::p ath"。正确的方法是什么?
我不断收到有关在主函数中使用isprime类成员函数"isprime::start"的错误消息,显然是因为它缺少对象,这对我来说似乎是不真实的。
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
class isprime{
public:
isprime();
void start(int thing);
bool test(int target);
void check(int x);
void path(int targ);
void checktwo(int xtwo);
operator()(int p);
private:
vector<int> testing;
int b;
};
int main ()
{
int given;
while(cin>>given)
{
isprime::start(given);//check input as long as there is input
}
return 0;
}
void isprime::start(int thing)
{
if(test(thing) == 1)
{
cout<<thing<<" is a prime number.";
}
else
{
check(thing);
}
}
isprime::isprime()
{
testing[0] = {2};
b = 0;
}
void isprime::check(int x)//checks if input is prime, and sets up next step if so
{
int s;
if(x == 0 || x == 1 || x == -1 || x == 2 || x == -2)
{
cout<<x<<" is a prime number.";
}
else
{
for(int i = 2; i < x; i++)
{
s = x % i;
if(s == 0)
{
b = 1;
break;
}
}
if(s != 0)
{
cout<<x<<" is a prime number.";
}
path(x);
}
}
bool isprime::test (int target)//see if input is already in list
{
for(int i = 0; i < testing.size(); i++)
{
if(target == testing[i])
{
return 1;
}
}
if(int i = testing.size() && target != testing[i])
{
return 0;//if not in list, must test whether it is prime
}
}
void isprime::path(int targ)
{
int y = testing.back() + 1;
while(y != targ)//find all primes between list end and input
{
checktwo(y);
y++;
}
testing.push_back(targ);//add prime input to vector
int storage = testing.size();//remember size
int z = targ + 1;
while(b = 1)//find the next prime while the target isn't prime
{
checktwo(z);
if(testing.size() != storage)//if the size changed, the next prime has been found
{
break;
}
z++;
}
}
void isprime::checktwo(int xtwo)//modified check function to add prime numbers between the vector end and the input to the vector
{
int s;
if( xtwo == -2 || xtwo == -1 || xtwo == 0 || xtwo == 1 || xtwo == 2)
{
testing.push_back(xtwo);
}
else
{
for(int i = 2; i < xtwo; i++)
{
s = xtwo % i;
if(s == 0)
{
break;
}
}
if(s != 0)
{
testing.push_back(xtwo);
}
}
}
operator()(const int p)
{
test(p);//calls a private member function to expand list of prime numbers (test)
}
-
声明
operator ()
时,需要指定返回类型。 -
当你定义它时,你需要用
isprime
-
调用
start
时需要一个对象
两个变化。在isprime
宣言中
bool operator()( const int p );
在定义中:
bool isprime::operator()( const int p )
{
return test(p);
}
编辑:如下所述。若要调用成员函数(非静态),必须具有该类型的对象。
int main ()
{
int given;
isprime prime_object;
while(cin>>given)
{
prime_object.start(given);//check input as long as there is input
}
return 0;
}
函数调用运算符是类 isprime 的成员,因此您应该指出:
bool isprime::operator()(const int p)
{
return test(p);
}
你编写代码的方式没有办法知道运算符是isprime
的成员。另外不要忘记为我推断的运算符指定一个返回类型,它应该是布尔值,这就是我上面使用的类型。
得到的第二个错误是因为您尝试使用没有实例的实例方法。创建 isprime
的实例并调用它的方法,或者将 start 声明为静态。从我所看到的情况来看,我想你需要一个实例。
相关文章:
- C++ 如何重载 [] 运算符并进行函数调用
- 检查类是否具有可能重载的函数调用运算符
- 避免在人为的重载函数调用中拼写出类型
- 为什么使用不匹配的参数调用重载函数仍然有效
- C++ 类的构造函数和函数调用运算符 () 重载之间的歧义
- C++编译时检查是否可以用某种类型的参数调用重载函数
- 在 if 语句中调用重载构造函数失败
- 重载运算符主体仅包含一个函数调用
- 为什么在析构函数中引发异常时不调用重载删除
- 重载<<运算符的函数调用 (C++)
- 从派生类调用重载函数
- 传递 const int* 和 int* 时调用重载函数的不同版本(const int* const&/&&)
- 将函数调用替换为重载运算符 C++
- C++ - 在 std::thread 中调用重载函数时编译失败
- 调用非静态函数的重载括号运算符
- 为什么为单个赋值操作调用复制构造函数和重载赋值运算符
- 使用重载运算符在函数调用时执行操作
- 重载的函数调用double不明确
- 从成员函数调用重载操作符(静态)
- 从默认构造函数调用重载构造函数时,维护对象状态信息