类无法找到另一个类(Separate Header Files):C ++

时间:2014-05-12 16:48:21

标签: c++

这是我的Pizaa.h

#pragma once
#ifndef PIZZA_H
#define PIZZA

#include <iostream>
#include <string>
#include <list>
#include <limits>
#include <vector>
#include <cstdlib>

using namespace std;


/****************************
 Abstract Base Pizza Class
*****************************/

class Pizza{
string name;
string dough;
string sauce;
std::list<string> topping;

    public:
Pizza(string name,string dough,string sauce);
void prepare();
virtual void bake()=0;
virtual void cut()=0;
void box();
string getName();
void addTopping(string str);
void getTopping(std::list<string>& lst);
virtual ~Pizza();
};

#endif

这是cheesePizza.h

#pragma once
#ifndef CHEEZEPIZZA_H
#define CHEEZEPIZZA_H

#include <iostream>
#include <string>
#include <list>
#include <limits>
#include <vector>
#include <cstdlib>
#include "Pizza.h"

using namespace std;

class cheesePizza:public Pizza{
public:
cheesePizza();
~cheesePizza();
virtual void bake();
virtual void cut();
    };

#endif

这是veggiePizza.h

#pragma once
#ifndef VEGGIEPIZZA_H
#define VEGGIEPIZZA_H

#include <iostream>
#include <string>
#include <list>
#include <limits>
#include <vector>
#include <cstdlib>
#include "Pizza.h"


class veggiePizza:public Pizza{
public:
veggiePizza();
~veggiePizza();
virtual void bake();
virtual void cut();
    };

#endif

这是我的工厂课程:

#include <iostream>
#include <string>
#include "Pizza.h"
#include "cheesePizza.h"
#include "veggiePizza.h"

using namespace std;

class SimplePizzaFactory{
public:
enum PizzaType {
         cheesePizza,
         veggiePizza
         };
Pizza* createPizza(PizzaType type)
{
    switch(type){
        case cheesePizza:
                       // Pizza* p = new cheesePizza(); ##Here
                       //delete(p); ##Here
        case veggiePizza:;
                         // return new veggiePizza();
        }
        throw "Invalid Pizza Type";
    }
    };



int main()
{

    Pizza* p = new cheesePizza();
    delete(p);
    return 0;
    }

我的SimplePizzaFactory方法createPizza()无法找到new cheesePizza()&amp; new veggiePizza()如果我取消注释#Here

,我会收到以下错误
$ g++ -Wall -c SimplePizzaFactory.cpp -o SimplePizzaFactory.o
SimplePizzaFactory.cpp: In member function ‘Pizza* SimplePizzaFactory::createPizza(SimplePizzaFactory::PizzaType)’:
SimplePizzaFactory.cpp:19:33: error: expected type-specifier before ‘cheesePizza’
                  Pizza* p = new cheesePizza();
                                 ^
SimplePizzaFactory.cpp:21:8: error: jump to case label [-fpermissive]
   case veggiePizza:;
        ^
SimplePizzaFactory.cpp:19:25: error:   crosses initialization of ‘Pizza* p’
                  Pizza* p = new cheesePizza();
                         ^
SimplePizzaFactory.cpp:17:8: warning: enumeration value ‘veggiePizza’ not handled in switch [-Wswitch]
  switch(type){

我的所有文件,*.o&amp; *.cpp放在同一个文件夹中。

我无法弄清楚为什么会出现这个错误。相同的代码 Pizza* p = new cheesePizza(); delete(p);内的main()工作正常,但如果您取消注释#Here,则会出现编译错误。

1 个答案:

答案 0 :(得分:2)

您将cheesePizza声明为枚举元素和类,可能枚举元素优先。

重命名其中一个以解决您的问题。

它在main中工作的原因是cheesePizza枚举元素超出了范围,所以那里没有nameclash。

如果您将程序简化为显示问题的最小示例,您很可能会自己发现问题。