问题描述
我刚开始使用单独的编译和 ADT,所以我创建了一个项目来了解如何创建不同类型的文件。我创建了三个文件
main.cpp(主文件)
#include "Interface.h" //include header file
#include <iostream>
#include <string>
using namespace std;
int main()
{
hello();
User user1;
}
#include "Interface.h"
#include <iostream>
#include <string>
using namespace std;
void hello()
{
cout << "Hello" << endl;
}
Interface.h(类、定义和数据变量所在的头文件)
#ifndef INTERFACE_H
#define INTERFACE_H
#include <string>
void hello();
class User
{
public:
string name;
int age;
};
#endif//INTERFACE_H
当我运行项目时,它给了我错误:
构建:在 FirstProject 中调试(编译器:GNU GCC 编译器) ===|
C:\Users\FirstProject\main.cpp|14|错误:未在此范围内声明“用户”|
||=== 构建失败:1 个错误,0 个警告(0 分钟,4 秒)
那是指我创建 user1.
的 main.cpp
有人可以展示我如何在主函数中声明 User 类。
我直接从我的代码中复制并粘贴了代码。这是我创建的名为 FirstProject 的项目中的所有内容。
解决方法
你可以用这个替换:
typedef struct User{
string name;
int age;
}