首页 > 其他分享 >VS2022编译错误:编译器错误 C2061

VS2022编译错误:编译器错误 C2061

时间:2022-10-04 18:22:23浏览次数:87  
标签:gof golf 错误 int void C2061 编译器 hc include

产生原因

  自己在做课后练习时,讲char类型替换为了string类型,编译器报错了很多错误,具体的代码如下:

golf.h

#pragma once

#include <string>//原本没有这两句会出错
using namespace std;//原本没有这两句会出错

struct gof
{

	string fullname;
	int handicap;
};

inline void set_golf(gof& g,string name, int hc);
inline int set_golf(gof& g);
inline void hanicap(gof& g, int hc);
inline void show_golf(const gof& g);

golf_fun.cpp

#include <iostream>
//#include <string>
#include "golf.h"

using namespace std;

//指定设置
void set_golf(gof& g, const string name, int hc)
{
	g.fullname = name;
	g.handicap = hc;
}

//输入设置
int set_golf(gof& g)
{
	int temp = 1;
	cout << "The fullname is: ";
	getline(cin,g.fullname);
	if (g.fullname=="")
	{
		temp = 0;
		return temp;
	}
	else
	{
		cout << "The handicap is: ";
		cin >> g.handicap;
		cin.get();
		return temp;
	}
}
//handicap设置
void hanicap(gof& g,int hc)
{
	g.handicap = hc;
}

//golf结构体打印
void show_golf(const gof& g)
{
	cout << "The fullname is: " << g.fullname << endl;
	cout << "The handicap is: " <<g.handicap<<endl;
}

gold_main.cpp

#include "golf.h"
#include "golf_fun.cpp"

using namespace std;

const int SIZE = 2;

int main(void)
{
	int cin_count = 0;
	gof g[SIZE];
	cout << "Please enter the information of golf" << endl;
	while (cin_count < SIZE)
	{
		set_golf(g[cin_count]);
		cout << "Please enter the next information of golf" << endl;
		cin_count++;
	}

	for (int i = 0; i < cin_count; i++)
	{
		show_golf(g[i]);
	}

	//修改值
	cout << "Reset all the players information: " << endl;
	for (int i = 0; i < cin_count; i++)
	{
		hanicap(g[i], 90);
		show_golf(g[i]);
	}
	return 0;
}

编译错误现象

解决过程

  1. 查看代码在修改的过程中是不是少标点符号了(没有)
  2. 查看官方提示“编译器错误 C2061”(好像是没有头文件)
  3. 尝试将string文件放在头文件声明,然后把using name space也放在里面(解决)

总结

  C++标准库中的函数或者对象都是在命名空间std中定义的,所以我们要使用标准函数库中的函数或对象都要使用std来限定,如果头文件中使用了这些声明,相应的在头文件必须添加命名空间的声明。

标签:gof,golf,错误,int,void,C2061,编译器,hc,include
From: https://www.cnblogs.com/YiMo9929/p/16754180.html

相关文章