【问题标题】:Import NetBeans C++ project to Visual Studio 2010将 NetBeans C++ 项目导入 Visual Studio 2010
【发布时间】: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


【解决方案1】:

class Creator
{
 public:
 Product* createObject(int month);
 private:
};

您没有指定任何private 成员。至少定义一个,或者删除private:

class Creator
{
 public:
 Product* createObject(int month);

};

Product* Creator::createObject(int month) {
    if (month >= 5 && month <= 9) {
        ProductA p1;
        return &p1;
    } else {
        ProductB p2;
        return &p2;
    }
}

当您返回本地对象的地址时,您将创建未定义的行为。 该错误表明您声明返回Product,但实际上您返回的是指向Product 的指针。你在这里复制粘贴有什么问题吗?

确保您的声明

Product* createObject(int month);

符合你的定义

 Product* Creator::createObject(int month) { ... }

我无法从这里发现错误...

编辑

看了你的代码,发现如下错误:

  • 您的 stdafx.h 被太多包含“毒害”,尤其是 using namespace std;-declarative,千万不要这样做!!!
  • 您没有为ProductAProductB 定义构造函数,结果是另一个错误
  • 不要将void 明确用作方法/函数的参数,这是C-style

虽然这听起来像是额外的工作,但尽量不要将namespace std 引入全局命名空间 -> 避免使用using namespace std;,尤其是在头文件中!

如果没有特别的理由创建带有预编译头的项目(stdafx.htargetver.h,不要这样做,因为这会使事情复杂化!)

我设法构建了您的项目,但使用的是 Visual Studio 2012 Express。如果您无法从我上传的文件中重新编译项目,请查看源文件并复制内容。

我将解决方案上传到我的SkyDrive-帐户。

如果这对您有帮助,请接受作为答案。

【讨论】:

  • 现在我只有 3 个错误和 1 个警告。错误 1 ​​错误 C2143:语法错误:缺少 ';' ''之前的错误 2 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int -2x 和一个警告 - 警告 C4183: 'createObject': missing return type;假定是一个成员函数,返回 'int' 在线 Product createObject(int month);请问有什么想法吗?所有项目都在那里mato.secit.sk/ProjektHPSolution.zip
  • 我在 stdafx.h 中更改了订单 #include "Product.h" 和 #include "Creator.h",现在我只有一个错误:错误 1 ​​错误 C1010:查找文件时意外结束预编译的头文件。您是否忘记在源代码中添加“#include "StdAfx.h""? - 在 Creator.cpp 中的代码下的空行上...
  • 对不起,我不明白 :-(
  • 好的,马托,我上传了一个解决方案。请查看我帖子中SkyDrive 的链接。
  • 非常感谢你。我编译了没有错误的解决方案。但是现在我运行了解决方案,我从系统中得到了这个错误:---------------------------- Microsoft Visual C++ Debug Library ----- ---------------------- 调试错误!程序:...path\Debug\Name.exe R6025 - 纯虚函数调用(按重试调试应用程序)------------------------ --- 取消 重试 忽略 ---------------------------
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多