【报错】关于[Error] unable to find string literal operator ‘operator“……的一个解决方案

此报错在cout中出现,因为漏打了"<<"

在做练习时出了这个报错:

[Error] unable to find string literal operator 'operator""x' with 'const char [3]', 'unsigned int' arguments

类似的还有:

[Error] unable to find string literal operator 'operator""x' with 'const char [18]', 'unsigned int' arguments

我以为是什么不可告人的大秘密,最后发现其实只是cout漏打了<<

补上了就没有问题了……

以下是发现问题、解决问题的大致过程,大家可以不看(因为都是废话)。

一、问题描述

在根据书本的代码进行练习时出了点问题。问题如下。

(一)、函数成员在类外部定义

#include <iostream>
using namespace std;

class sample
{
	public:
		sample(int x1,int y1)
		{
			x = x1;
			y = y1;
			//cout<<"调用构造函数\tx = "x<<",y = "<<y<<endl;
		}
		~sample()
		{
			cout<<"调用析构函数\n";
		}
		
		void print();
		/*
		{
			cout<<"x="<<x<<"\ty="<<y<<endl;
		}
		*/
		
		
	private:
		int x, y;	
	
};


void sample::print()
{
	cout<<"x="x<<", y="<<y<<endl;
}

int main()
{
	int i;
	sample array[3] = { sample(10,20), sample(30,40), sample(50,60) };
	for(i = 0; i<3; i ++)
	{
		array[i].print();
	}
	return 0;
}

将display函数在类外部、主函数之前定义,于是出现报错:

[Error] unable to find string literal operator 'operator""x' with 'const char [3]', 'unsigned int' arguments

 

我将print函数放回public中,于是就没有报错:

#include <iostream>
using namespace std;

class sample
{
	public:
		sample(int x1,int y1)
		{
			x = x1;
			y = y1;
			//cout<<"调用构造函数\tx = "x<<",y = "<<y<<endl;
		}
		~sample()
		{
			cout<<"调用析构函数\n";
		}
		
		void print()
		{
			cout<<"x="<<x<<"\ty="<<y<<endl;
		}
		
	private:
		int x, y;	
	
};


int main()
{
	int i;
	sample array[3] = { sample(10,20), sample(30,40), sample(50,60) };
	for(i = 0; i<3; i ++)
	{
		array[i].print();
	}
	return 0;
}

(二)、构造函数里cout<<x;

同样还是这个问题,我将

//cout<<"调用构造函数\tx = "x<<",y = "<<y<<endl;

取消注释,于是出现如下报错:(和上面报错信息的唯一区别是这里为const char[18])

[Error] unable to find string literal operator 'operator""x' with 'const char [18]', 'unsigned int' arguments

我本以为是x, y都在这里出错,但我把x删除后它就没有报错了:

 非常神奇,留这个坑等待以后某一天填上。

二、问题解决

本人显然没有解决问题,但根据其报错提示,我猜测与以下方面有关。

  • const char[]
  • unsigned int
  • 作用域运算符::

(明天上课问问老师……)

破案了,这里写错了……少写了<<,晕,还是要仔细啊

哈哈,你不报错谁报错……

所以和上面列举的几个都没有关系,单纯是我漏打符号了……

  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值