C++知识点

一、控制台不跳出

system("pause");

需要引用:#include <string>

二、头文件

  为了与 C兼容,C++保留了 C语言中的一些规定。其中之一是头文件的形式。

C 语言中头文件用.h 作为后缀,如 stdio.h, math.h, string.h 等。

在C++发展初期,为了和 C语言兼容,许多 C++编译系统保留了头文件以.h 为后缀的用法,如 iostream.h。

但后来ANSI C++建议 头文件不带后缀.h,近年退出的C++编译系统新版本采用了C++的新方法,提供了一批不带.h的头文件,如用iostream,string等作为头文件名。

两种都可以用。

Note

由于 C语言无命名空间,因此用带后缀的.h 的头文件时不必用“using namespace std;”作声明。

三、输出控制台显示中文乱码

  试下:开始执行(不调试)  

若不行的话,首先看下CMD命令行的活动页:

究其原因,可能是 系统中的 "非unicode程序的语言"( “Language for non-Unicode programs”)
虽然设置界面显示为中文(即:code page:936),但实际上变成了英文(即:code page 437). 设置成其他的后,再恢复,就解决问题。

参考:window7 cmd无法输入汉字问题、无效代码页问题的解决

在控制面板中将“系统区域”改成了“英语(美国)”,将系统重启;然后再将系统区域设置改成“中文(简体,中国)”,重启,问题解决,久违的汉字终于回来了。

 更多参考:https://blog.csdn.net/qq_35343446/article/details/79489917

 

四、C++中添加类

以Point类为例:
键入Point会显示出一个.h文件,一个.cpp文件,点击完成,会出现一个源文件,一个头文件。
1、头文件:在Point.h文件中编入代码(类的定义),是关于一些成员函数的声明 #Pragma once 是表示只读一次
2、源文件:主要是Point.h中的函数的实现,要引入Point.h头文件,所以要写入#include“Point.h"

五、引入OpenCV编译链接错误

  1、hello.cpp(2): fatal error C1083: Cannot open include file: 'opencv2/opencv.hppp': No such file or directory

一个是头文件的文件名拼写错误;

二是未将头文件所在路径添加到开发环境中。项目属性中设置~

2、链接

1>hello.obj : error LNK2019: unresolved external symbol "class  cv::Mat __cdecl cv::imread(class std::basic_string<char,struct

std::char_traits<char>,class std::allocator<char> > const &,int)"

(?imread@cv@@YA?AVMat@1@ABV?$basic_string@DU?$char_traits@D@std@@  V?$allocator@D@2@@std@@H@Z) referenced in function _main

“unresolved external symbol”,更具体的意思是在 main 函数中使用了 imread 函数,但是无法从外部找到 imread

opencv.hpp 头文件告诉编译器有个 imread 函数可以用,编译通过;但是到了连接时,连接器却找不到 imread的具体实现,故出错。

要解决这一问题,需要将依赖的库文件添加到项目设置中。

六、struct和typedef struct

分两块来讲述:
  1 首先://注意在C和C++里不同
    在C中定义一个结构体类型要用typedef:
    typedef struct Student
    {
    int a;
    }Stu;
    于是在声明变量的时候就可:Stu stu1;(如果没有typedef就必须用struct Student stu1;来声明)
    这里的Stu实际上就是struct Student的别名。Stu==struct Student
    另外这里也可以不写Student(于是也不能struct Student stu1;了,必须是Stu stu1;)
    typedef struct
    {
    int a;
    }Stu;
    

但在c++里很简单,直接
    struct Student
    {
    int a;
    };    
    于是就定义了结构体类型Student,声明变量时直接Student stu2;
======================================================================================

  2.其次:
    在c++中如果用typedef的话,又会造成区别:
    struct   Student   
    {   
    int   a;   
    }stu1;//stu1是一个变量  

 
    typedef   struct   Student2   
    {   
    int   a;   
    }stu2;//stu2是一个结构体类型=struct Student  

 
    使用时可以直接访问stu1.a
    但是stu2则必须先   stu2 s2;
    然后               s2.a=10;

  

原文地址:https://www.cnblogs.com/peterYong/p/6556551.html