Tag Archives: c++

error C2146: syntax error : missing ‘;’ before identifier ‘fd’

Compiler error: error C2146: Syntax error: Missing ‘; ‘ before identifier ‘fd’
Follow up my_socket FD in mySQL_com.h file; /* For Perl DBI/ DBD */ line, found that the compiler does not recognize my_socket, but my_socket is defined in mysql.h: #define my_socket SOCKET, so the next is the SOCKET definition. SOCKET definition is in Winsock2.h, but with the predefined # IFDEF ___, the header winsock2.h is not included at all.
Solution: In #include< mysql.h> With # include< winsock2.h> Can!

vector length_error

Reserve Length error’ STD ::length_error’ What (): Vector ::_M_fill_insert
The reason is that the reserved length (resize() parameter of the vector function) is incorrect.
Such as:
Examples in the c++ reference documentation.

// length_error example
#include <iostream>       // std::cerr
#include <stdexcept>      // std::length_error
#include <vector>         // std::vector

int main (void) {
  try {
    // vector throws a length_error if resized above max_size
    std::vector<int> myvector;
    myvector.resize(myvector.max_size()+1);
  }
  catch (const std::length_error& le) {
	  std::cerr << "Length error: " << le.what() << '\n';
  }
  return 0;
}

is resize assigns the maximum length +1,
there are V.r esize (n.) not sure, but n (code appear the error, not for n assignment, all can sometimes run, sometimes when n is negative)
references: cplusplus.com/reference/stdexcept/length_error/

 

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

In the process of using Python development, it is often necessary to install various modules, but in the process of installation, various errors are often encountered. For example, error: 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 error
One is to directly download the corresponding WHEEL file for installation and provide three download addresses:
Tsinghua download (domestic site, faster) : https://pypi.tuna.tsinghua.edu.cn/simple/pip/
The 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 component.
For the second solution, I believe most people will download a Visual Studio 201X and install it, as I did before. However, installing a VS takes up a lot of space, not to say the key, but it is still unusable after installation, which is really annoying.
After a variety of search, later found that you can directly install the official C++ runtime can be a perfect solution, leave an address: Microsoft Visual C++ Build Tools 2015
 
Double-click to run after downloading and install using default Settings. After the installation is complete, proceed to PIP Install XXX to indicate a successful installation.

 
Welcome to my personal blog: The Road to Machine Learning

Apawn class, add axis mapping to control apawn movement

APawn class, add axis mapping to enable control of APawn movement
MyPawn.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/StaticMeshComponent.h"
#include "Camera/CameraComponent.h"
#include "MyPawn.generated.h"

UCLASS()
class FIRSTDEMO_API AMyPawn : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	AMyPawn();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	UPROPERTY(VisibleAnywhere)
	UStaticMeshComponent* MyStaticMesh;

	UPROPERTY(VisibleAnywhere)
	UCameraComponent* MyCamera;

	UPROPERTY(EditAnywhere)
	float MaxSpeed;//add pawn movement speed parameter.

private:
	// add axis mapping function, MoveForward, MoveRight, Velocity
	//Set to private variables, handled in the Pawn class itself, no need to expose them to outsiders.
	void MoveForward(float Value);
	void MoveRight(float Value);
	FVector Velocity;

};

MyPawn.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyPawn.h"
#include "Components/InputComponent.h"

// Sets default values
AMyPawn::AMyPawn()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	RootComponent=CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));

	MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
	MyStaticMesh->SetupAttachment(GetRootComponent());

	MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
	MyCamera->SetupAttachment(GetRootComponent());

	MyCamera->SetRelativeLocation(FVector(-300.f, 0.f, 300.f));
	MyCamera->SetRelativeRotation(FRotator(-45.f, 0.f, 0.f));

	AutoPossessPlayer = EAutoReceiveInput::Player0;
	
	MaxSpeed = 100.f;
	Velocity = FVector::ZeroVector;

}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	//Add offset function to tick.
	AddActorLocalOffset(Velocity * DeltaTime, true);

}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	
	// Bind player input to axis mapping, axis mapping and function name should correspond to each other
	PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AMyPawn::MoveForward);
	PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AMyPawn::MoveRight);

}

void AMyPawn::MoveForward(float Value)
{
	//Forward speed, use Clamp to limit value to -1,1, place too large a value in the Ue4 editor, resulting in a bug.
	Velocity.X = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}

void AMyPawn::MoveRight(float Value)
{
	// Speed to the right, use Clamp to limit value to -1,1, place too large a value in the Ue4 editor, resulting in a bug.
	Velocity.Y = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}


error C2061: syntax error : identifier ‘BinryTreeNode’

1> D :\ My documentation \ Visual Studio 2005\ Tree \ Tree \tree\tree.h(23) : Error C2061: Syntax Error: Identifier ‘BinryTreeNode’

1> D :\ Visual Studio 2005\ Projects \ Tree \ Tree \ Tree.h (77) : See reference to class Template Instantiation
‘BinaryTree< T> ‘ being compiled

At the error indicated in the first line ‘BinaryTree< T> ‘Writing error

error: expected unqualified-id before ‘.’ token

C++ introduced the mutex header, which USES mutext.lock() to lock and mutex.unlock() to release the lock.

#include <mutex>

using namespace std;

mutex t_mutex;

class Csingleob
{
        private:
                Csingleob(){}
        static Csingleob *p;

        public:
                static Csingleob* getInstance()
                {
                        mutex.lock();
                        if (p == NULL)
                        {
                                p = new Csingleob();
                        }
                        mutex.unlock();
                        return p;
                }
};

However, an error is reported at compile time. Prompt:

error: expected unqualified-id before ‘.’ token    mutex.lock();

It is an error to use mutext.lock() to lock, and mutex.unlock() to release the lock. You should instantiate the class or structure first, then call the corresponding method with “.”
After modification:

#include <mutex>

using namespace std;

mutex t_mutex;

class Csingleob
{
        private:
                Csingleob(){}
        static Csingleob *p;

        public:
                static Csingleob* getInstance()
                {
                        t_mutex.lock();
                        if (p == NULL)
                        {
                                p = new Csingleob();
                        }
                        t_mutex.unlock();
                        return p;
                }
};

Csingleob* Csingleob::p = NULL;

 
 
 
 

Various errors (c + +)

OS X EI Capitan [10.11.6]
Xcode [8.0]
C++
Code:

#include< iostream>
#include< math.h>
#include”MyComplex.h”
usingnamespacestd;
int main(int argc,constchar * argv[]) {
//insert code here…
MyComplex c1;
MyComplex c2.
Cin & gt; > c1;
C2 = c1;
Cout & lt; < c1 < < endl;
Cout & lt; < c2 < < endl;
MyComplex c3.
C3 = c1 + c2; //error1:No viable overloaded ‘=’
Cout & lt; < c1+c2 < < Lendl; //error2: No matching constructor for initialization of ‘MyComplex’
Cout & lt; < c1-c2 < < Lendl; //error
Cout & lt; < c1/c2 < < Lendl; //error
Cout & lt; < c1*c2 < < Lendl; //error
    
Return0;
}

So how is the overloading of = defined?Look at the code.

MyComplex& operator = (MyComplex & rhs){
Real = RHS. GetReal ();
Imaginary = RHS. GetImaginary ();
Return * this;
}

It doesn’t seem to be a problem. By looking at http://www.cplusplus.com/forum/general/153830/ to find the reason of the error:
A temporary object can’t bind to A non-const reference.
(A temporary object cannot be bound to an extraordinary reference, that is, the bound reference must be constant)
Therefore, the parameter of the overloaded = function must be const. (Such a covert mistake is more than enough to cry about…)
Error1 disappears after adding const:

MyComplex& operator = (constMyComplex & rhs){
Real = RHS. GetReal ();
Imaginary = RHS. GetImaginary ();
Return * this;
}
Error2 has the same error cause.

Friend ostream& operator< < (ostream& os,const MyComplex & c);
Const is very important! Want to! !!! After the correction, the errors disappeared

C++:error C2228: left of ‘.str’ must have class/struct/union

Error C2228: Left of ‘. STR ‘must have class/struct/union

#include <string>
#include <iostream>
#include <vector>

using namespace std;

template <typename T>
class ValueBox {
private:
    T value;

private:

    template<typename U, 
        typename std::enable_if<std::is_class<U>::value && !std::is_same<U, string>::value, U>::type* = nullptr, 
        typename std::enable_if<!class_str<U>::ret, U>::type* = nullptr>
    std::string str(const T&) {
        cout << "1.---------------------" << endl;
        return "null";
    };

    template<typename U, 
        typename std::enable_if<std::is_class<U>::value && std::is_same<U, string>::value, U>::type* = nullptr>
    std::string str(const T&) {
        cout << "2.---------------------" << endl;
        return value;
    };

    template<typename U, 
        typename std::enable_if<std::is_class<U>::value && !std::is_same<U, string>::value, U>::type* = nullptr, 
        typename std::enable_if<class_str<U>::ret, U>::type* = nullptr>
    std::string str(const T&) {
        cout << "3.---------------------" << endl;
        return value.str();
    };

    template<typename U, 
        typename std::enable_if<!std::is_class<U>::value && std::is_arithmetic<U>::value, U>::type* = nullptr>
    std::string str(const T&) {
        cout << "4.---------------------" << endl;
        return std::to_string(value);
    };

public:
    ValueBox(const T& _value) : value(_value) {
    }

    const T& getValue() const {
        return value;
    };

    T& getValue() {
        return value;
    };

    std::string str() {
        return str<T>(value);
    };
};


int main() {
    ValueBox<string> s("sddds");
    cout << s.str() << endl;

    ValueBox<bool> j ( true);
    cout << j.str() << endl;

    ValueBox<int> i(100);
    cout << i.str() << endl;

    ValueBox<float> f ( 10.6f);
    cout << f.str() << endl;

    ValueBox<Box> b1 (Box());
    cout << b1.str() << endl;

    ValueBox<Bin> b2 (Bin());
    cout << b2.str() << endl;

    return 1;
}

The error was caused by the C++ compiler putting ValueBox< Box> b1 (Box()); In b1, Box() is regarded as the function definition, and Box() is regarded as the parameterless function with the return value of Box, except the function name is not defined at this time. There is a common name for the mistake: Most Vexing Parse
The modification methods are as follows:
1. Put Box on a separate line and initialize
Box b; // Box B () cannot be used; In this case, b is defined as a function
ValueBox< Box> b1 (b);
2. Wrap Box() in parentheses so that Box() is not treated as a function
ValueBox< Box> b1 ((Box()));
3. Use the uniform initialization syntax in C++11, that is, use {}
ValueBox< Box> b1 {Box()};
 

–Be alert for C++’s most vexing parse
Item 6. Be alert for C++’s most vexing parse
Effective STL note: Item 6–Be alert for C++’s most vexing parse

[tools] vscode debug report G + +. Exe no such file or directory fatal error no input files

Yesterday, we set up the debugging environment of VSCode Cpp breakpoint. It could still run at noon, but it started to report errors in the evening. According to the environment set up by the article I published, when I tried to debug by pressing F5, the error was reported as follows:


watch VSCode integrated in the TERMINAL error
g + +. The exe: error: e: ProjectsVSCodeCppTest5srctest. CPP: No to the file or directory
here's e: ProjectsVSCodeCppTest5srctest. CPP should actually be e: \ Projects \ VSCode \ CPP \ Test5 \ SRC \ test CPP.
but is resolved to an address without a slash, so the compiler cannot find the file.
Turns out, the reason for this error parse is because. Different terminals parse slashes and backslashes differently. Yesterday, after configuring the environment, I happened to find that VS Code integrated terminal itself, and also integrated all terminals in the environment, including PowerShellCommand Prompt (CMD) , and Git Bash. When using the shortcut key CTRL + shift + ~ to open the terminal, click the drop-down arrow on the right to select the default open terminal.

click Select Default Shell, all shells in the environment will pop up at the top of the interface for selection.

select Git Bash and F5 debug Cpp code, it will report the opening error. When you select PowerShell or Command Prompt, you are ready to debug normally!
Note that the default shell cannot be selected by clicking the drop-down menu when
error is reported. You need to open a terminal by yourself with the shortcut key.

error: `cout’ was not declared in this scope

Error compiling C++ using GCC under Linux:

#include< iostream>
int main()
{
cout < < “Hello World!” < < endl;
return 0;
}

compiler error:
$g++ s.> s.bbpp: In function ‘int main(int, char**)’:
The



s.cpp:12: error: `cout’ was not declared in this scope

s.cpp:12: error: `endl’ was not declared in this scope

The reason:

C++ 1998 requires cout and endl to be called using ‘STD ::cout’ and’ STD ::endl’ formats, or using namespace STD;

Revised:

#include< iostream>

int main()

{
std::cout < < “Hello World!” < < std::endl;
return 0;
}

or

#include< iostream>

using namespace std;

int main(int argc, char *argv[])

{

cout < < “hello world” < < endl;
return 0;
}

Compile through.