Tag Archives: c++

In C + + cin.getline The difference between () and getline () functions

cin.getline ():
usage: receive a string, which can receive spaces and output. It needs to include & lt; CString & gt;

char m[20];
cin.getline(m,5);
cout<<m<<endl;

Input: jkljkljkl
output: jklj

Receive 5 characters into m, and the last one is’ \ 0 ‘, so only 4 characters can be output;

Extension:
1 cin.getline () actually has three parameters, cin.getline (variables of receiving string, number of receiving characters, end characters)
2. When the third parameter is omitted, the system defaults to ‘\ 0’
3 cin.getline () to read cin.getline When jlkjkljkl is input, jklj is output; when jkaljkljkl is input, JK is output

Getline():
usage: to receive a string, which can receive spaces and output. It needs to include & lt; CString & gt;

string str;
getline(cin,str);
cout<<str<<endl;

Input: jkljkljkl
output: jkljkljkl

Input: JKL jfksldfj jklsjfl
output: JKL jfksldfj jklsjfl

PIP installation error: Microsoft Visual C + + 14.0 is required perfect solution

In the process of using Python development, we often need to install various modules, but we often encounter various errors in the installation process, such as: error: Microsoft Visual C + + 14.0 is required, this error is due to the lack of C + + compiler components.

There are two solutions to this problem

One is to directly download the corresponding wheel file for installation, providing three download addresses:

Tsinghua download (domestic site, fast speed) https://pypi.tuna.tsinghua.edu.cn/simple/pip/

Official download: https://pypi.org/project/face-recognition/1.0.0/#files

Python package https://www.lfd.uci.edu/~gohlke/pythonlibs/

Another solution is to install the missing components.

As for the second solution, I believe most people will download a visual studio 201x and install it as I used to. However, installing a vs takes up a lot of space, not to mention the key. Some people find it useless after installation, which is really irritating.

After all kinds of search, it was found that the C + + runtime provided by the government can be directly installed, which can be solved perfectly. The address is Microsoft Visual C + + build tools 2015

 

After downloading, double-click run to install with default settings. After the installation, go to PIP install XXX to show that the installation is successful.

 

Welcome to my personal blog: the road of machine learning

Failed to talk to init day

When using WSL in the morning, an error was reported:

root@PC0:/home/zhang# systemctl start ssh.service
System has not been booted with systemd as init system (PID 1). Can't operate.

This error report is easy to solve. The solution is to use Service :

root@PC0:/home/zhang# service ssh status
 * sshd is running

Then I try to shut down:

root@sidanzhang-PC0:/home/zhang# poweroff
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to talk to init daemon.

True pit, shut down all report wrong.
According to this answer: rebooting Ubuntu on windows without rebooting windows?- super user
the solution is to open a new window of PowerShell and run the command:

wsl --shutdown

If your win10 system has not been updated, the administrator can run the following command:

net stop LxssManager

The crash of delete after QT removeitem in qgraphicsscene and the display residue

Problem description

In the process of using QT’s graphicsview framework to create and interact with user-defined items, you need to remove unnecessary user-defined items. However, after the user-defined items interact with the re implemented rotation and scaling, removeitem () + delete will occasionally cause the error of accessing illegal memory, and even if you only use removeitem (), there will occasionally be residual items in the view.

Solutions

Using setitemindex method of qgraphicsscene (qgraphicsscene:: Noindex);
to disable quick query of index can solve the above problem.

other

For the problem of whether to delete after using removeitem(), the official question has given a clear explanation

The ownership of item is passed on to the caller (i.e., qgraphics scene will no longer delete item when destroyed).
remove the item and all its children from the scene. Ownership of the item is passed to the caller (that is, qgraphicsscene will not delete the item when it is destroyed).

So remember to delete

glCreateProgram error

Troubleshut< opengl>:

Problem Description:

Today, I encountered an OpenGL error. I doubted my life. The error report is as follows:
glcreateprogram() is a function in glew. I thought that the library connection was wrong. Then I connected all kinds of glew32.lib to the computer for a long time. By chance, I found that glew had no init and fainted…

Solution:

Execute before calling:
/* initialize glew. */
glewexperimental = GL_ TRUE;
GLenum err = glewInit();
if (err != GLEW_ OK)
{
std::cout << “Error initializing GLEW: ” << glewGetErrorString(err)
<< std::endl;
std::exit(EXIT_ FAILURE);
}

C + + common errors: “error: XXX in namespace ‘STD’ does not name a template type”

Error “error: XXX in namespace ‘STD’ does not name a template type”
is reported

Or “error: XXX does not name a type”

Cause of error

The corresponding header file was not imported

resolvent

Add the corresponding header file

string

#include <string>

list

#include <list>

vector

#include <vector>

function

#include <functional>

Analysis of shadows a parameter exception in C + +

string test1(string &str1){

   

string str1=”hello1″;

    return str1;

}

As shown in the above code, the exception of declaration of ‘STD:: string STR1’ shadows a parameter will be reported when compiling. The reason for the exception is the naming conflict between & amp; STR1 and STR1.

[C + +] C + + overload operator = must be a nonstatic member function?

code

#include <iostream>
using namespace std;

class C {
public:
    int x;
    C () {}
    C(int a) : x(a) {}
    //  member function
    C operator = (const C&);
};

C C::operator= (const C& param) {
    x = param.x;
    return *this;
}

int main()
{
    C foo(1);
    cout <<"foo.x = " << foo.x << endl;

    C bar;
    bar = foo;
    cout <<"bar.x = " << bar.x << endl;
    return 0;
}

run

foo.x = 1
bar.x = 1

ERROR

opeartor= must be a nonstatic member function

note

quote

Notice that some operators may be overloaded in two forms: either as a member function or as a non member function
many operators can be overloaded as member function or non member function

explain

The so-called member function is shown in the code section. There is a simple declaration about the operator to be overloaded in the class definition, such as:

    C operator = (const C&);

Here the operator = (equal sign) is overloaded;

In contrast, non member function does not exist in the class definition. For example, the complete definition of a class consists of the following:

class D {
public:
    int y;
    D () {}
    D (int b) : y(b) {}
};

There are many operators in C + +, such as = (equal sign), which can only be overloaded as member function. In other words, they must be declared in the class definition. See code;

At the same time, some operators, such as + (plus sign), can be overloaded as both member function and non member function.

doubt

The example code in the part of toturial [1] classes (II) / the keyword this that I read is as follows:

CVector& CVector::operator= (const CVector& param)
{
  x=param.x;
  y=param.y;
  return *this;
}

Note that it is written as cvector & amp; , referring to the class C , then for = (equal sign), it should be written as C & amp; , but in this way, the compiler (Dev C + + ISO C + + 11) will report an error and modify it to the final code before it can be compiled. This is the inconsistency between the current code and the example code.

reference

Classes (II)
http://www.cplusplus.com/doc/tutorial/templates/

Error: transfer of control bypasses initialization of: variable XXX solution

Error: transfer of control bypasses initialization of: variable XXX

The nature of the problem, causes and Solutions

The nature of the problem

The code may skip the initialization of some variables, which makes the program access the uninitialized variables and crash.

Causes

I encountered this problem when porting the code from the vs compiling environment of windows to the G + + of Linux (actually compiling CUDA C + + code with nvcc, but the nvcc background also calls G + + to compile C / C + + part of the code). Later, it was found that in vs environment, it was just a warning, but in G + +, it was an error, so this problem must be solved.

terms of settlement

    whether the variables in the analysis code are initialized, whether the variables are declared after the goto statement in the analysis code, and if so, move the variable declaration to the front of goto .
// error
goto SomeWhere;
int var = 10;
// right
int ver = 10;
goto SomeWhere;
    analyze whether or switch statements appear in the code, because switch statements are essentially implemented with goto , so the above problems may also exist. In addition to reference 2, write the variable declaration before switch, add curly brackets to each branch of switch , or change the switch statement to if / else .
// error
switch (choice)
{
    case 1:
        // do something
        break;
    case 2: 
        // do something
}
// right
switch (choice)
{
    case 1:
    {
        // do something
        break;
    }
    case 2: 
    {
        // do something
    }
}
// or
if(choice == 1)
{
	// do something
}
else if(choice == 2)
{
	// do something
}