C++报错集锦
(一)invalid initialization of non-const reference of type ‘float&’ from a temporary of type ‘float’
代码如下:
#include <iostream>
using namespace std; void update(float& i) {
cout << i << endl;
} void g(double d, float r) {
update(2.0f);
update(r);
update(d);
} int main(void) {
g(3.2, 1.3);
}
原因在于调用update(2.0f)和update(d)出错了:
对于普通的T&的初始式必须是一个类型T的左值;
对非const引用参数不允许做类型转换,非const引用不能引用一个常量。
这个问题有待进一步讨论。
(二)error: ‘cout’ was not declared in this scope
这真是个弱智问题,在文件头部加上
#include <iostream>
using namespace std;
(三)usestock0.cpp:7: error: request for member ‘acquire’ in ‘fluffy_the_cat’, which is of non-class type ‘Stock*’
这是因为自己用的是:
Stock *fluffy_the_cat = new Stock;
fluffy_the_cat.acquire("NanoSmart", 20, 12.50);
fluffy_the_cat.show();
fluffy_the_cat.buy(15, 18.125);
fluffy_the_cat.show();
问题出在fluffy实际上指针,而我在用的时候却把它当作普通的变量
解决办法:
Stock *fluffy_the_cat = new Stock;
(*fluffy_the_cat).acquire("NanoSmart", 20, 12.50);
(*fluffy_the_cat).show();
(*fluffy_the_cat).buy(15, 18.125);
(*fluffy_the_cat).show();
果然可以了,其实我是想测试如果用new新建对象,但是又不delete,这时候会不会调用析构函数,
最后是没有调用析构函数
(四)error: call of overloaded ‘max(int&, int&)’ is ambiguous
正在练习函数模版,结果报错
li7_2.cc: In function ‘int main()’:
li7_2.cc:19: error: call of overloaded ‘max(int&, int&)’ is ambiguous
li7_2.cc:7: note: candidates are: T max(T, T) [with T = int]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:209: note: const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]
代码如下:
#include <iostream>
#include <string> using namespace std; // 声明了一个类模版
template <typename T> T max(T a, T b)
{
return a > b ? a : b;
} int main()
{
int a, b;
cout << "Input two integers to a and b: ";
flush(cout);
endl(cout);
cin >> a >> b;
cout << "max(" << a << "," << b << ")=" << max(a,b) << endl;
}
原来是出现了函数实例模糊,std里面也有一个max函数模版,真是晕了,程序搞不清要用哪个,但是书上的程序居然是通的。真是醉了。
于是只好把那个max改为max1,程序就可以运行了。
(五)error: invalid operands of types ‘const char [2]’ and ‘char’ to binary ‘operator<<’
一般这个错误是cout << 输出时,<<少写了一个<
cout << "max1(" << "\'" << c << "\'" << "," <"\'" << d << "\'" << ")=" << max1(c,d) << endl;
(六)error: non-member function ‘T max(T*, int)’ cannot have cv-qualifier
错误代码
template <typename T> T max(T a[], int n) const
{
T tmp = a[0];
for(int i=1; i<n; i++)
{
if(a[i] > tmp)
tmp = a[i];
}
return tmp;
}
这里由于template max是一个函数模板,但是我却在后面加了一个const,天哪,const放在函数后面只能用于成员函数,表名调用这个函数的对象在函数执行的过程中并不改变。
而且函数模版的声明和定义必须是全局作用域,模板不能被声明成类的成员函数。
C++报错集锦的更多相关文章
- react+typescript报错集锦<持续更新>
typescript报错集锦 错误:Import sources within a group must be alphabetized.tslint(ordered-imports) 原因:impo ...
- Python ——报错集锦
https://blog.csdn.net/weixin_42660771/article/details/80990665 错误(1):SyntaxError:'return' outside fu ...
- 源码安装zabbix遇到的报错集锦
报错1:checking for mysql_config... configure: error: MySQL library not found 解决办法:查找mysql_config #find ...
- hexo报错集锦
1.报错信息如下 FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubles ...
- python在linux的报错集锦
1. 报错提示 /usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 ...
- javascript报错集锦
1.JS 异常之 missing ) after argument list 错误释疑报错原因:不是字符串就输出啦
- vue、vuex、iview、vue-router报错集锦与爬坑记录
1.vue报错: 没安装 less-loader css-loader style-loader 可能的很大原因:没安装less 2.vuex报错:Computed property &qu ...
- django2.0集成xadmin0.6报错集锦
1.django2.0把from django.core.urlresolvers修改成了django.urls 报错如下: 1 2 3 File "D:\Envs\django-xad ...
- eclipse本地启动tomcat报错集锦
1.eclipse本地添加tomcat服务器 打开Eclipse,单击“window”菜单,选择下方的“Preferences”: 找到Server下方的Runtime Environment, ...
随机推荐
- 在MyEclipse中改动jsp页面的默认打开方式
在JavaWeb项目中.当然有非常多jsp页面,可是我发现,双击打开jsp页面总是卡机.相对于打开其它java文件而言非常慢,感觉非常不舒服.MyEclipse中默认打开jsp页面是以可视化的形式展现 ...
- Intellij IDEA 设置代码着色
从visual studio到Eclipse再到Intllij IDEA,其实每个编译器都有自己的风格,但是就美观上来说,个人觉得还是visual studio的代码着色比较美观,exlipse次之, ...
- eclipse背景颜色调整参考(绿色养眼哟),其他工具也可以设置
http://hi.baidu.com/630270730/blog/item/d64f64dcc05376385982ddf6.html 提示键配置.提示快捷键.提示背景色.关键字颜色.代码显示.编 ...
- LINUX使用DVD光盘或者ISO作为本地YUM源
先把光盘或 ISO 文件挂载到文件系统: # mkdir /media/iso 挂载光盘: # mount /dev/cdrom /media/iso 或挂载 ISO 文件: mount -o loo ...
- C#实现相似QQ的隐藏浮动窗口、消息闪动
功能简单介绍 当语音客服系统登录成功进入主界面时,本聊天工具将会自己主动隐藏在左下角位置,当鼠标移动到左下角时,自己主动弹出,当鼠标移开聊天窗口时,自己主动隐藏.假设想让聊天窗口固定在桌面.仅仅要拖动 ...
- 利用eChart绘制网页图表
首先,最好的教程在这里:eCchart eChart所需JS: echarts.min.js china.js echarts.js 页面代码如下: 一.图表 <!DOCTYPE html> ...
- java读取url中json文件中的json数据
有时候需要远程从其他接口中获取json数据,如果遇到返回的json数据是一个文件而不直接是数据,那么可以通过以下方法进行读取: /** * 从数据接口获取到数据 * @return * @throws ...
- Linux操作系统CentOS7.2发行版本的安装与配置(安装是选择服务器类型)
原文地址:http://1130739.blog.51cto.com/1120739/1738791 由于CentOS 7出来不久,对于这个版本的系统安装与配置的教程较少,今天就索性介绍一下CentO ...
- html input select等控件宽度对不齐?请使用box-sizing。
今天终于明白为什么表单元素的宽度经常对不齐了,原来是计算的问题. 为元素设置:box-sizing: border-box;,则表明width的宽度为内容本身宽度+padding+border(高度一 ...
- 窗体彻底关闭事件FormClosed
//Application.Exit()是退出整个应用程序 Application.ExitThread();//强制中止调用线程上的所有消息,同样面临其它线程无法正确退出的问题 System.Dia ...