【问题标题】:namespace::function cannot be used as a functionnamespace::function 不能用作函数
【发布时间】:2016-12-09 13:45:12
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    这是函数声明中的错误:

    int checkPrimeness(num);
    

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

    int checkPrimeness(int);
    

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

    【讨论】:

    • 谢谢!我仍然有一些错误,我在 main.cpp 中没有更改任何内容,并且在 Primes.h 和 Primes.cpp 文件中将“num”更改为“int”。 (我还看到我不小心使用了两次 count 所以我为此创建了一个新的“评估”变量)。但是,它仍然告诉我所有变量都有多个定义;并且只使用 int 作为形式参数,我将如何通过实际参数传递 num 的值?我是否必须将它放在一个类中,因为这将是一个成员变量?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 2023-03-25
    • 2019-01-14
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多