您的位置:首页 > 其它

Something about Compile error

2016-04-09 14:46 711 查看
A strange compile error!

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 object

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

#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) );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: