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, ...
随机推荐
- 【微信小程序】用户首次进入小程序拒绝授权,如何再次调用授权页面,获取用户信息userInfo
前言:微信小程序的app.js里面,最少有2个接口,一个wx.login:一个是wx.getUserInfo: 前者得到腾讯给我们的微信用户唯一的code,通过code获取openid,这个不需要用户 ...
- mysql中的order by
一.order by的原理 1.利用索引的有序性获取有序数据 当查询语句的 order BY 条件和查询的执行计划中所利用的 Index 的索引键(或前面几个索引键)完全一致,且索引访问方式为 ran ...
- 兼容火狐,ie8的 js urlencode和urldecode
function UrlEncode(str)//url编码{ var i,temp,p,q; var result=""; for(i=0;i<str.length;i++ ...
- JFinal连接数据库配置说明
本文采用的是加载配置文件的形式和数据库进行交互 ps:数据库采用的是postgresql 1.加载配置文件 public void configConstant(Constants me) { Pro ...
- python学习笔记011——闭包
1 定义 定义:在计算机科学中,闭包是词法闭包的简称,是引用了自由变量的函数 简单地说:闭包就是能够读取其他函数内部变量的函数,闭包是将函数内部和函数外部连接起来的桥梁.——来源百度百科 2 描述 形 ...
- bash 基本功能
1 shell概述 shell是一个命令解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序.用户可以用shell启动.挂起.停止甚至是编写一些程序. shell是一个功能强大 ...
- Accounting_会计电算化工作指南
会计电算化工作指南 会计电算化实施的内容目标及原则 企业会计电算化的实施,也就是企业建立会计电算化的整个过程,是一项复杂的系统工程.在整个系统的实施过程中,包括会计电算化工作的规划,会计信息的建立与管 ...
- android 自定义ViewSwipeBackHelper,实现左滑结束Activity
https://github.com/Jude95/SwipeBackHelper Git上看到一个基于SwipeBackLayout的实现,可以让我们在使用过程中在不使用物理返回键的情况下舍去了返 ...
- [置顶] Android中使用Movie显示gif动态图
转载请注明: http://blog.csdn.net/u012975705/article/details/48717391 在看这篇博文之前对attr自定义属性还是不是很熟的童鞋可以先看看:An ...
- 最短路径 - 弗洛伊德(Floyd)算法
为了能讲明白弗洛伊德(Floyd)算法的主要思想,我们先来看最简单的案例.图7-7-12的左图是一个简单的3个顶点的连通网图. 我们先定义两个二维数组D[3][3]和P[3][3], D代表顶点与顶点 ...