【问题标题】:"type 'int' unexpected" in constructor, Can't find WHY?构造函数中的“类型'int'意外”,找不到为什么?
【发布时间】:2012-06-04 13:45:12
【问题描述】:

我对 C++ 和他的语法很陌生。我以前用 C# 编程过,但想试试 C++。

我在 Visual Studio 中创建了一个 ClassLibrary,并想向其中添加一些类。我知道这是托管 C++。

但我无法理解为什么我不断收到这些错误:

Error C2062: type 'int' unexpected
Error C2143: syntax error: messing ; before '{'
Error C2447: '{' : missing function header (old-style formal list?)

这是我的头文件:

// LibraryLib.h

#pragma once
#include <string>
using namespace System;

namespace LibraryLib {

public enum EntryType {Book, Newspaper};

public ref class Entry
{   
public:
    int id;
    int year;
    String ^ title;
    EntryType type;

    Entry(int Id, int Year, String ^ Title, EntryType Type);

};
}

这是我的 cpp 文件:

// This is the main DLL file.

#include "stdafx.h"
#include "LibraryLib.h"

namespace LibraryLib {

LibraryLib::Entry(int Id, int Year, String ^ Title, EntryType Type) // line of errors   
{
    id = Id;
    year = Year;
    title = Title;
    type = Type;
}
}

在我想在cpp文件中实现构造函数的那一行抛出了3个错误。

我希望有人能指出我做错了什么。

谢谢!

【问题讨论】:

  • 他的语法?我一直认为 C++ 是一个。曾经的妖精,有着炽热的性情淬炼出的裸机速度的魅力,是凡人无法理解的。

标签: visual-studio class constructor c++-cli managed-c++


【解决方案1】:

你没有限定构造函数的权利。你需要另一个Entry::

Entry::Entry(int Id, int Year, String ^ Title, EntryType Type)

【讨论】:

  • 事实上LibraryLib:: 是不需要的,因为它被包装在一个命名空间块中。
  • 啊好吧,我明白了。非常感谢:D
【解决方案2】:

为您的构造函数代码尝试 Entry::Entry(int Id, int Year, String ^ Title, EntryType Type)

【讨论】:

  • 请正确格式化代码。默认字体很难阅读。
猜你喜欢
  • 1970-01-01
  • 2011-07-18
  • 2010-12-12
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多