【发布时间】:2013-05-25 14:31:56
【问题描述】:
你能帮我解决 Visual C++ 中的这些错误吗?我是 C++ 新手,我从 NetBeans 导入了这段代码(设计模式工厂)。在 NetBeans 中,此代码是正确的。但现在我需要在 Microsoft Visual Studio 2010 中编译这段代码,它会产生这些错误:
创作者.h
#pragma once
#include "stdafx.h"
class Creator
{
public:
Product* createObject(int month);
private:
};
错误:
- 错误 C2143:语法错误:缺少 ';'在“”上线之前 - 产品 createObject(int month)
- 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int 在线 - Product* createObject(int month);
Creator.cpp
#include "stdafx.h"
Product* Creator::createObject(int month) {
if (month >= 5 && month <= 9) {
ProductA p1;
return &p1;
} else {
ProductB p2;
return &p2;
}
}
错误:
IntelliSense:声明与“Creator::createObject(int mesic)”不兼容(在第 9 行声明 - 这是:Product createObject(int month);)
stdafx.h:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
using namespace std;
#include "Creator.h"
#include "Product.h"
#include "ProductA.h"
#include "ProductB.h"
产品.h:
#pragma once
#include "stdafx.h"
class Product
{
public:
virtual string zemePuvodu() = 0;
Product(void);
~Product(void);
};
产品.cpp:
它只有:
#include "stdafx.h"
Product::Product(void)
{
}
Product::~Product(void)
{
}
谢谢你的回答。
【问题讨论】:
-
你的
Product班级在哪里? -
删除
#include "stdafx.h"行。换成class Product;。这足以满足Product*的使用。 -
我从 Creator 中删除了私有,但它并没有改变什么。
-
我在 Creator.cpp 中用“Creator.h”替换了 stdafx.h,现在我只有 3 个错误和一个警告。错误 1 错误 C2143:语法错误:缺少 ';'在“”之前,错误 2 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int -2x 和一个警告 - 警告 C4183: 'createObject': missing return type;假设是一个成员函数返回'int'在线产品 createObject(int month);
标签: c++ visual-studio visual-c++ netbeans