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

标签 function c++11 compiler-errors namespaces syntax-error

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/

相关文章:

function - Julia:效率和指示参数类型

c++ - 在 std::for_each 中使用 std::bind

c++ - 常量和重载构造函数

compiler-errors - AttributeError : 'Model' object has no attribute 'predict_classes'

打印二维字符数组的 C 函数

javascript - 在 JavaScript 中动态定义函数名称?

c++ - vector::push_back 坚持使用复制构造函数,尽管提供了移动构造函数

c++ - 为什么编译器在定义类似的模板特化时不会报错?

java - 无法从 teaminfo 类型对非静态方法acceptadd(String) 进行静态引用

c++ - 如何制作自定义关键字语句