namespace :: function不能用作函数

时间:2016-12-09 13:45:13

标签: 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;
    }
}

Primes.h

#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和几乎所有我能想象到的东西。

1 个答案:

答案 0 :(得分:0)

这是函数声明中的错误:

int checkPrimeness(num);

定义使用 num 初始化的全局整数变量 checkPrimeness !要声明一个函数,你应该改为:

int checkPrimeness(int);

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