gpt4 book ai didi

function - 命名空间::function cannot be used as a function

转载 作者:行者123 更新时间:2023-12-02 11:00:54 25 4
gpt4 key购买 nike

main.cpp

#include "Primes.h"
#include <iostream>

int main(){
std::string choose;
int num1, num2;
while(1 == 1){
std::cout << "INSTRUCTIONS" << std::endl << "Enter:" << std::endl
<< "'c' to check whether a number is a prime," << std::endl
<< "'u' to view all the prime numbers between two numbers "
<< "that you want," << std::endl << "'x' to exit,"
<< std::endl << "Enter what you would like to do: ";
std::cin >> choose;
std::cout << std::endl;
if(choose == "c"){
std::cout << "Enter number: ";
std::cin >> num1;
Primes::checkPrimeness(num1) == 1 ?
std::cout << num1 << " is a prime." << std::endl << std::endl :
std::cout << num1 << " isn't a prime." << std::endl << std::endl;
}else if(choose == "u"){
std::cout << "Enter the number you want to start seeing primes "
<< "from: ";
std::cin >> num1;
std::cout << "\nEnter the number you want to stop seeing primes "
<< "till: ";
std::cin >> num2;
std::cout << std::endl;
for(num1; num1 <= num2; num1++){
Primes::checkPrimeness(num1) == 1 ?
std::cout << num1 << " is a prime." << std::endl :
std::cout << num1 << " isn't a prime." << std::endl;
}
}else if(choose == "x"){
return 0;
}
std::cout << std::endl;
}
}

素数
#ifndef PRIMES_H
#define PRIMES_H

namespace Primes{
extern int num, count;
extern bool testPrime;
// Returns true if the number is a prime and false if it isn't.
int checkPrimeness(num);
}

#endif

Primes.cpp
#include "Primes.h"
#include <iostream>

int Primes::checkPrimeness(num){
if(num < 2){
return(0);
}else if(num == 2){
return(1);
}else{
for(count = 0; count < num; count++){
for(count = 2; count < num; count++){
if(num % count == 0){
return(0);
}else{
testPrime = true;
if(count == --num && testPrime == true){
return(1);
}
}
}
}
}
}

我收到以下3个错误: Errors from terminal

我已经花了数小时的时间,但似乎仍然无法修复错误。
我已经尝试过使用extern和几乎所有我能想象的东西。

最佳答案

这是函数声明中的错误:

int checkPrimeness(num);

定义一个全局整数变量 checkPrimeness ,初始化为 num !要声明一个函数,您只需将其更改为:
int checkPrimeness(int);

无法理解为什么将参数声明为外部变量。要拆分声明和实现,您应该在头文件中声明所有函数和类,并在源文件中定义它们。

关于function - 命名空间::function cannot be used as a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41061875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com