gpt4 book ai didi

c++ - Visual Studio 在 stdafx.h 之前包含 header 会产生错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:32 24 4
gpt4 key购买 nike

<分区>

假设我有一个类 Person

测试类.h:

#pragma once

class Person {
int age;
std::string name;

public:
Person(int age, const std::string& name);

private:
int getAge();
std::string getName();
};

测试类.cpp

#include "stdafx.h"
#include "TestClass.h"

Person::Person(int age, const std::string& name) : age(age), name(name) {};

int Person::getAge() {
return age;
}

std::string Person::getName() {
return name;
}

对于这个例子,代码编译。但是,如果我在 TestClass.cpp 中的第 1 行和第 2 行之间切换并包含stdafx.h 其次,出于某种原因我得到以下编译错误:

'Person': is not a class or namespace name  
missing type specifier - int assumed. Note: C++ does not support default-int
'Person': constructor initializer lists are only allowed on constructor definitions
'Person': is not a class or namespace name
'Person': function should return a value; 'void' return type assumed
'age': undeclared identifier
'Person': is not a class or namespace name
'name': undeclared identifier

显然,如果我先包含 stdafx.h,我就不会有任何问题,但我似乎无法理解为什么会这样。任何帮助将不胜感激。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com