operator用法

示例:

  • main.cpp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33

    #include<iostream>
    #include"operator_test.h"
    using namespace std;
    MyInt::MyInt(int a, int b)
    {
    m_a = a;
    m_b=b;
    }

    void MyInt::operator -()
    {
    m_a = -m_a;
    m_b = -m_b;

    }


    void MyInt::print()
    {
    cout << "m_a=" << m_a << endl;
    cout << "m_b=" << m_b << endl;
    }

    int main()
    {

    MyInt F(14, -25);
    cout << "取负前:" << endl;
    F.print();
    -F; //相当于: F.operator-();
    cout << "取负后:" << endl;
    F.print();
    }
  • operator_test.h

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class MyInt
    {
    private:
    int m_a;
    int m_b;
    public:
    MyInt(int a, int b);
    void operator - ();
    void print();


    };

函数重载与定义

  1. 函数重载示例
  • mian.cpp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    #include <stdio.h>
    #include<iostream>
    #include"overload.h"
    using namespace std;


    Tdate::Tdate()
    {
    month = 6;
    day = 20;
    year = 2002;
    cout << month << "/" << day << "/" << year << endl;
    }

    Tdate::Tdate(int m, int d)
    {
    month = m; day = d; year = 1979;
    cout << month << "/" << day << "/" << year << endl;
    }

    Tdate::Tdate(int m, int d, int y)
    {
    month = m; day = d; year = y;
    cout << month << "/" << day << "/" << year << endl;

    }

    void main()
    {

    Tdate adate;
    Tdate bdate(7, 2);
    Tdate cdate(4, 20, 2006);
    }
  • overload.h

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    class Tdate
    {
    private:
    int month;
    int day;
    int year;
    public:
    Tdate();
    Tdate(int m, int d);
    Tdate(int m, int d, int y);
    };
  1. 函数再定义
  • RefinitionFunction.h

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include<iostream>
    using namespace std;
    class Student
    {
    public:
    void display() { cout << "Student::display()" << endl; }
    };


    class MiddleStudent :public Student
    {
    public:
    void display() { cout << "MiddleStudent::display()" << endl; }
    };

    class GraduateStudent :public MiddleStudent
    {
    public:
    void display() {
    cout << "GraduateStudent::displayu()" << endl;}
    };
  • main.cpp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    #include <stdio.h>
    #include<iostream>
    #include"RefinitionFunction.h"
    using namespace std;

    void main()
    {

    Student s;
    MiddleStudent ms;

    GraduateStudent gs;
    s.display();
    ms.display();
    gs.display();

    cout << "------------------------------" << endl;
    Student *ps;
    ps = &s;
    ps->display();
    ps = &ms;
    ps->display();
    ps = &gs;
    ps->display();

    cout << "------------------------------" << endl;

    MiddleStudent *take;
    take = &ms;
    take->display();

    }

【OutPut】

1
2
3
4
5
6
7
8
9
10
Student::display()
MiddleStudent::display()
GraduateStudent::displayu()
------------------------------
Student::display()
Student::display()
Student::display()
------------------------------
MiddleStudent::display()
Press any key to continue . . .

  1. This function or variable may be unsafe
    vs2015报错:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Severity	Code	Description	Project	File	Line	Suppression State
    Error C4996 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. DLL_ZBLCommunication d:\gx_2017\2017-07\0\dll_zblcommunication\dll_zblcommunication\zblcommunication.cpp 156



    Severity Code Description Project File Line Suppression State
    Error C4996 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. DLL_ZBLCommunication d:\gx_2017\2017-07\0\dll_zblcommunication\dll_zblcommunication\zblcommunication.cpp 504


    Severity Code Description Project File Line Suppression State
    Error C4996 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. DLL_ZBLCommunication d:\gx_2017\2017-07\0\dll_zblcommunication\dll_zblcommunication\zblcommunication.cpp 531

解决:

  1. 工程属性添加预处理命令
    在属性页面中找到“C/C++”——”预处理器“,按下图操作单击箭头指向处的按钮

在下面的编辑窗口中添加一句命令:_CRT_SECURE_NO_WARNINGS
添加完成后应用并退出

  1. 我在代码开头添加

    1
    #define _CRT_SECURE_NO_WARNINGS
  2. unexpected token(s) preceding;'type 'int' unexpectedthis declaration has no storage class or type specifer

解决:在出错代码前加

1
#include<Windows.h>


  1. ‘<<’没有找到std::string重装数
    1
    Error	1	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)	g:\vs_project\vs_2013\consolewein32_vs2013\consolewein32_vs2013\vs2013_main.cpp	20	1	consolewein32_VS2013

解决办法:
加上头文件#include<string>

  1. ntelliSense: no default constructor exists for class “Base”

报错代码:
C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 #include <iostream>
using namespace std;

class Base
{
private:
int n;

public:
Base(int m) :n(m){ cout << "constructor is called\n"; }
~Base(){}
};

class Derive :public Base
{
private:
int n;

public:
Derive(int m) :n(m)
{
Base(3);
}
~Derive(){}
};

int main()
{

Derive* a = new Derive(10); return 0;
}

解决:

  1. 两个类有继关系,父类没有显示声明无参构造数,但声明了有参数构函数,必须用参数列列表初始化;
    参照文章:C++中必须用参数列表初始化的4种情况
  1. 在基类里添加无参数构造函数;
  • 在xp+vs2005使用 Winbase.h头文件报错:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    CGxmisCommunication.cpp
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(226) : error C2146: syntax error : missing ';' before identifier 'Internal'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(226) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(226) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(227) : error C2146: syntax error : missing ';' before identifier 'InternalHigh'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(227) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(227) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(230) : error C2146: syntax error : missing ';' before identifier 'Offset'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(230) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(230) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(231) : error C2146: syntax error : missing ';' before identifier 'OffsetHigh'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(231) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(231) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(234) : error C2146: syntax error : missing ';' before identifier 'Pointer'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(234) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(234) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(237) : error C2146: syntax error : missing ';' before identifier 'hEvent'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(237) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(237) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(241) : error C2146: syntax error : missing ';' before identifier 'nLength'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(241) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(241) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(242) : error C2146: syntax error : missing ';' before identifier 'lpSecurityDescriptor'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(242) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(242) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(243) : error C2146: syntax error : missing ';' before identifier 'bInheritHandle'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(243) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(243) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(247) : error C2146: syntax error : missing ';' before identifier 'hProcess'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(247) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(247) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(248) : error C2146: syntax error : missing ';' before identifier 'hThread'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(248) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(248) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(249) : error C2146: syntax error : missing ';' before identifier 'dwProcessId'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(249) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(249) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(250) : error C2146: syntax error : missing ';' before identifier 'dwThreadId'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(250) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(250) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(261) : error C2146: syntax error : missing ';' before identifier 'dwLowDateTime'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(261) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(261) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(262) : error C2146: syntax error : missing ';' before identifier 'dwHighDateTime'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(262) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(262) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(272) : error C2146: syntax error : missing ';' before identifier 'wYear'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(272) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(272) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(273) : error C2146: syntax error : missing ';' before identifier 'wMonth'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(273) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(273) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(274) : error C2146: syntax error : missing ';' before identifier 'wDayOfWeek'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(274) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(274) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(275) : error C2146: syntax error : missing ';' before identifier 'wDay'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(275) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(275) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(276) : error C2146: syntax error : missing ';' before identifier 'wHour'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(276) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(276) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(277) : error C2146: syntax error : missing ';' before identifier 'wMinute'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(277) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(277) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(278) : error C2146: syntax error : missing ';' before identifier 'wSecond'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(278) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(278) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(279) : error C2146: syntax error : missing ';' before identifier 'wMilliseconds'
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(279) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(279) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(283) : error C2065: 'WINAPI' : undeclared identifier
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(283) : error C2065: 'PTHREAD_START_ROUTINE' : undeclared identifier
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(283) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(283) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Generating Code...
    Compiling...
    Serial.cpp

解决:
不能直接使用#include

应该改为#include

  • Error C1083 Cannot open include file: ‘windows.h’: No such file or directory
    出错环境 win10+vs2015+Berkeley DB XML 6.0.18

实际解决:

把工程属性中VC++ Directories中选项:Library DirectoriesInclude Diretories设置Berkeley DB XML 6.0.18相应路径


  • C1073: Internal error involving incremental compilation

Severity Code Description Project File Line Suppression State
Error C1073 Internal error involving incremental compilation(compiler file ‘f:\dd\vctools\compiler\cxxfe\sl\p1\c\p0io.c’, line 997) UnitTest d:\github\crcpp\inc\crc.h 1

解决:
工程属性—> Configuration Properties——>C/C++——–>Code Generation—–>Enable Minimal Rebuild :No(/Gm-)

vs2005使用Berkeley DB XML 2.3.10编程的时候报连接错误

在使用C++编程的时候连接问题

1
2
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Dbt::~Dbt(void)" (??1Dbt@@QAE@XZ) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Dbt::Dbt(void)" (??0Dbt@@QAE@XZ) referenced in function _main

工程属性设置:
Configuration Properties—>C/C++填上:
C:\Program Files\Oracle\Berkeley DB XML 2.3.10\include

Configuration Properties—>Linker—> Input 填上: libdb45d.lib

参考文章
http://www.91linux.com/Berkeley_DB/2014/0131/5719.html

boost配置报错

  1. LNK1104
    1
    1>LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc100-mt-gd-1_57.lib

原因:
boost库编译时用的link=shared runtime-link=shared threading=multi,所以生成的是 boost_thread-vc100-mt-gd-1_57.dll和导出库 boost_thread-vc100-mt-gd-1_57.lib。

解决办法:
project –> Property—-> Configuration Properties—>C/C++—->Preprocessor–(右边选项卡)——>Preprocessor Definitions—(添加)—->BOOST_ALL_DYN_LINK

参考[C/C++]使用boost时无法连接到正确的库,提示fatal error LNK1104


  1. boost程序运行报错找不到动态库
1
2
3
4
5
6
The code execution cannot proceed because boost_thread-vc100-mt-gd-1_57.dll was not found.Reinstalling the  program may fix this  problem.

同样报错的还有:
+ boost_date_time-vc100-mt-gd-1_57.dll

+ boost_system-vc100-mt-gd-1_57.dll

解决:
把缺失的dll, 放入程序运行目录下即可解决


Fork me on GitHub