类未在作用域中声明

Class Not Declared in Scope

本文关键字:声明 作用域      更新时间:2023-10-16

当我尝试编译main时收到"未在此范围内声明油门"错误.cpp。我对 c++ 很陌生,所以请耐心等待。我在两个 cpp 文件的标头中都有#include "throttle.h",所以我不确定为什么当我尝试创建节流对象时没有声明......

主.cpp文件:

#include <stdio.h>
#include "throttle.h"
using namespace std;
int main(int argc, char **argv)
{
throttle throt1(5, 0);
throttle throt2(4, 0);
return 0;
}

throttle.h 文件:

#ifndef MAIN_SAVITCH_THROTTLE 
#define MAIN_SAVITCH_THROTTLE     
namespace main_savitch_2A
{
class throttle
{
public:
    // CONSTRUCTORS
    //throttle( );
    //throttle(int size);
    throttle(int size = 1, int start = 0); //by adding this default 
                                           //constructor the other two 
                                           //are not needed
    // MODIFICATION MEMBER FUNCTIONS
    void shut_off( ) { position = 0; }
    void shift(int amount);
    // CONSTANT MEMBER FUNCTIONS
    double flow( ) const 
    { 
        return position / double(top_position); 
        }
    bool is_on( ) const 
    { 
        return (position > 0); 
    }
    int get_top_position()const;
    int get_position()const;
    friend bool operator <(const throttle& throt1, const throttle& throt2);
    //postcondtion: returns true if flow of throt1 < flow of throt2. 
    //return false if flow of throt1 > flow of throt2
private:
    int top_position;
    int position;
};
}
#endif

油门.cpp文件 :

#include <cassert>     // Provides assert
#include "throttle.h"  // Provides the throttle class definition
using namespace std;   // Allows all Standard Library items to be used
namespace main_savitch_2A
{
//throttle::throttle( )
//{   // A simple on-off throttle
    //top_position = 1;
    //position = 0;
//}
//throttle::throttle(int size)
// Library facilities used: cassert
//{
    //assert(size > 0);
    //top_position = size;
    //position = 0;
//}
throttle::throttle(int size, int start)
{
    assert(size > 0);
    assert(start = 0);
    top_position = size;
    position = start;
}
void throttle::shift(int amount)
{
position += amount;
if (position < 0)
    position = 0;
else if (position > top_position)
    position = top_position;
}
bool operator <(const throttle& throt1, const throttle& throt2)
{
    return(throt1.flow() < throt2.flow());
}
int throttle::get_top_position()const
{
    return top_position;
}
int throttle::get_position()const
{
    return position;
}
}

在你的主方面,它应该是main_savitch_2A::throttle throt1(5, 0);
throt2也是如此.

有关更多详细信息,请参阅命名空间。