Something about Compile error
2016-04-09 14:46
711 查看
A strange compile error!
It occurs When a class is defined in the header file that contains no added the semicolon:
main.cpp:10: error: cannot call member function ‘int counter1::count()’ without object
main.cpp:13: error: cannot call member function ‘void counter2::count()’ without object
main.cpp:16: error: no match for call to ‘(counter1) ()’
counter2.h:12: error: ‘int counter2::counter’ is private
main.cpp:18: error: within this context
counter2.h:12: error: invalid use of non-static data member ‘counter2::counter’
main.cpp:18: error: from this location
With a return value and arguments this might make a bit more sense:
error :expected unqualified-id before ‘using’
It occurs When a class is defined in the header file that contains no added the semicolon:
Class xxxx { ... }; // that is the semicolon.
#include<iostream> #include"counter1.h" #include"counter2.h" using namespace std; int main() { counter2::set(3); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { counter1::count(); } for (int i = 0; i < m; i++) { counter2::count(); } counter1 x; x(); cout << counter1::counter << endl; cout << counter2::counter << endl; }
// wrong code: #include<iostream> using namespace std; class counter2 { public: void set(int x) { counter = x; } void count() { counter++; } private: int counter; }; class counter1 { public: int count(); static int counter; }; int counter1::count() { counter++; }
// After modify the code. #include<iostream> namespace counter2 { int counter = 0; void set(int x) { counter = x; } void count() { counter++; } }; class counter1 { public: counter1() {} ~counter1() { counter = 0; } void operator()() { /*The first () is the name of the operator - it's the operator that is invoked when you use () on the object. The second () is for the parameters, of which there are none.*/ // error: no match for call to ‘(counter1) ()’ ++counter; } static void count(); //error: cannot call member function ‘void counter1::count()’ without object,add "static"is ok static int counter; }; void counter1::count() { counter++; } int counter1::counter = 0; //Without this sentence,it will be "In function `counter1::count()', undefined reference to `counter1::counter'"
compile error:
main.cpp:6: error: cannot call member function ‘void counter2::set(int)’ without objectmain.cpp:10: error: cannot call member function ‘int counter1::count()’ without object
main.cpp:13: error: cannot call member function ‘void counter2::count()’ without object
main.cpp:16: error: no match for call to ‘(counter1) ()’
counter2.h:12: error: ‘int counter2::counter’ is private
main.cpp:18: error: within this context
counter2.h:12: error: invalid use of non-static data member ‘counter2::counter’
main.cpp:18: error: from this location
#include <iostream> #include <string> using namespace std; //using namespace编译指示,使在C++标准类库中定义的名字在本程序中可以使用 //否则,iostream,string 等c++标准类就不可见了,编译就会出错。 //两个在不同命名空间中定义的名字相同的变量 namespace myown1{ string user_name = "myown1"; } namespace myown2{ string user_name = "myown2"; } int main() { cout<< "/n" << "Hello, " << myown1::user_name //用命名空间限制符myown1访问变量user_name << "... and goodbye!/n"; cout<< "/n" << "Hello, " << myown2::user_name //用命名空间限制符myown2访问变量user_name << "... and goodbye!/n"; return 0; } //用命名空间限制符访问变量 //Another example: #include<iostream> using namespace std; namespace Cui{ void Love() { cout<<"He love a wonderful girl, and her name is SHILI"<<endl; } } using namespace Cui; int main() { int a=10; cout<<a<<endl; Love(); Cui::Love();//调用方式2 return 0; }
What’s more~
The first part operator() is the way to declare the function that is called when an instance of the class is invoked as a function. The second pair of parentheses would contain the actual arguments.
With a return value and arguments this might make a bit more sense:
class Adder{ public: int operator()(int a, int b){ //operator() -- this is the "name" of the operator // in this case, it takes two integer arguments. return a+b; } }; Adder a; assert( 5==a(2,3) );
相关文章推荐
- CCF-201509-1 数列分段
- 自定义EL函数库
- 直接拿来用!最火的Android开源项目
- HTML几个标签使用
- Coredata — 入门使用
- input-dev输入子系统 .
- Android ViewStub的应用
- S5P4418启动模式分析
- 分治法之最大子数组问题
- [IO]——重定向
- 深入理解javascript原型和闭包系列 深入理解javascript原型和闭包(13)-【作用域】和【上下文环境】
- Linux安装ftp组件
- 比特币0.9.2中文版编译手册
- c# Linq Lamda表达式使用 GroupBy 分组
- 网页设计必不可少的控件
- 指针函数与函数指针--两个简单例子进行说明
- [Unity游戏开发]向量在游戏开发中的应用(三)
- 《深入理解计算机系统》第七章学习笔记
- HTML概述
- Java基础之——泛型(一)