我对 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;
}
}
这 3 个错误是在 cpp 文件中我想要实现构造函数的行上抛出的。
希望有人能指出我做错了什么。
谢谢!
最佳答案
您没有正确限定构造函数。您需要其中另一个 Entry::
:
Entry::Entry(int Id, int Year, String ^ Title, EntryType Type)
关于visual-studio - "type ' int ' unexpected"在构造函数中,找不到为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10882406/