Something about Compile error

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) );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值