Tag Archives: C++ Error

[Solved] C++ Error: terminate called after throwing an instance of ‘char const‘

0. Error Codes:

//insert
template <class DataType>  
void SeqList<DataType> :: Insert(int i, DataType x)
{
	if (i >= MaxSize)  throw "up";
	if (i < 1 || i > length + 1)  throw "The postions is not right";
	for (int j = length; j >= i; j--)
		data[j] = data[j - 1];             //Note that the jth element exists at the j-1 subscript of the array
	data[i - 1] = x;
	length++;
}


//main function
int main(){
	int r[5]={1, 2, 3, 4, 5};
	SeqList<int> L(r, 5);
	cout<<"The data after performing the insert operation is:"<<endl;
	L.PrintList( );              //output all elements
	try{
//		L.Insert(2,10);
		L.Insert(10,30);
	}catch(char *e){
		cout<<e<<endl; 
	}
	cout<<"The data after performing the insert operation is:"<<endl;
	L.PrintList( );              //output all elements

In fact, the code is still relatively easy, it is an insertion problem in an array, in an inserted array in the wrong position, it will report an exception to handle. But in this program the exception is not caught, but the program will stop for a long time in the middle and show terminate called after throwing an instance of ‘char const*’, the reason for this situation is that the exception does not match up in the catch, the C++ destructor throws The exception will automatically call terminate() to terminate the program. Then how to solve this situation?

1. Problem-solving
In fact, it is very simple to solve the problem, just add const in front of char *e in catch, here char *e is turned into a string constant pointer.

int main(){
	int r[5]={1, 2, 3, 4, 5};
	SeqList<int> L(r, 5);
	cout<<"The data before performing the insert operation is:"<<endl;
	L.PrintList( ); //output all elements
	try{
// L.Insert(2,10);
// The reason for the problem is that the exception type does not match and c++ starts its own exception handling
// Solution: just add const in front of char to make the caught exception variable a constant 
		L.Insert(10,30); // the second parameter 30 will cause an exception to be thrown (i > length + 1)  
	}catch(const char *e){
		cout<<e<<endl; 
	}
	cout<<"After performing the insert operation the data is:"<<endl;
	L.PrintList( ); //output all elements

}

 

[Solved] C++ Error: Undefined symbols for architecture x86_64:

C++ Error:

Undefined symbols for architecture x86_64:
“StackMy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::~StackMy()”, referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Lastly, it was found that the error was caused by a custom destructor in the header file, but the destructor was not implemented.

C# Error: Import “google/protobuf/timestamp.proto“ was not found or had errors. [How to Solve]

When using c# as the development language to convert Pb files into CS files, I believe many people will encounter a very difficult problem

The first question: in the protoc3 environment, import timestamp. In the header, import “Google/protobuf/timestamp. Proto”; Exceptions will be thrown when: Google/protobuf/timestamp. Proto “was not found or had errors;

Solution [sharing of original articles by blogger “pamxy”:

(Note: it was found later that it is not necessary to add this directory, because the timestamp.pb.cc file generated by timestamp.proto has been compiled as the source code when compiling libprotobuf.lib file, and libprotobuf.lib is also used in compiling protoc.exe, so it is natural to default that there is already a source code, so there is no need to import it again!)
Just delete the import “Google/protobuf/timestamp. Proto”.

Second question:  ” google.protobuf.Timestamp” is not defined.

Under normal circumstances, there is no need to import google.protobuf.timestamp directly in the protoc3 environment, because in the compilation process, the problem will be read in the Lib file, but if timestamp is called in the file, it is as follows:

It is necessary to call the timestamp file in the header, but bloggers are always prompted during the call  ” google.protobuf.Timestamp” is not defined.

There is really no way, so I have to find the path of this file: timestamp.proto file in protobuf master \ SRC \ Google \ protobuf folder, directly copy the file to the same level directory of the file you want to compile, and then modify the timestamp file in the header. The call path: Import “timestamp. Proto”;

Finally, the file was finally solved…….

The third question: how to call after converting the protocol file into a CS file?

a. Found in referenced project: Tools & gt& gt; Nuget package manager & gt& gt; Nuget package for management solution & gt& gt; Search for “Google. Protocolbuffers” and install

B, directly convert the protoc file into the CS file, and call it in the project.

This small problem is recorded, which is also convenient for you to use as a reference when you encounter this problem.

[Solved] C++ error: undefined reference to `xxx‘

        Error: undefined reference to ` xxx ‘in C + + means that an undefined method is referenced. There are many reasons for this problem. Here are two problems I encountered.

1. The corresponding header file is not referenced or the versions of library functions referenced in the header file are inconsistent. In different versions of libraries, the names of the same implementation method may be inconsistent, which causes this problem.

2. The method of using extern keyword is wrong. There are many uses of extern. The function of extern here is to refer to functions of other files.

a.h:

//a.h

#ifndef AH
#define AH

extern int test();//Here the return type and parameters must be the same as the implementation method

#endif

aaa.cpp

//aaa.cpp

#include <stdio.h>
#include <iostream>
#include "a.h"  //Both need to refer to the corresponding header file, otherwise an error will be reported

using namespace std;

int test()
{
cout << "abc" <<endl;
}

bbb.cpp

//bbb.cpp

#include <stdio.h>
#include <iostream>
#include "a.h" //Both need to refer to the corresponding header file, otherwise an error will be reported


int main()
{
    test();
}

Results of operation:

If any cpp file does not refer to the corresponding header file, an error will be reported.

How to Fix error: conversion from “” to non-scalar type “”

Error: the conversion from ‘STD: : _List_const_iterator & lt; _Mylist> To non-scalar Type ‘STD ::_List_iterator< _Mylist> ‘the requested
Error C2440 resolved: “Initialize” : cannot be retrieved from “STD ::_List_const_iterator< _Mylist & gt;” Into “STD: : _List_iterator & lt; _Mylist & gt;”
Writing C++ code often USES const as a function argument, which is easy to do when using iterator if the variable is of type STL or contains type STL.

void list_print(const list<int> &list)  
{  
    for (list<int>::iterator iter = list.begin();
         iter != list.end();
         ++iter) {
        ...    
     }
}  

The following error will be reported in this case

error C2440: “initialize”: cannot “std::_List_const_iterator<_Mylist>” to “std::_List_iterator<_Mylist>”
or
error: conversion from 'std::_List_const_iterator<_Mylist>' to non-scalar type 'std::_List_iterator<_Mylist>' requested

Here, because list itself is of type const, we need to use a const-type iterator, which is list::const_iterator
Code to

void list_print(const list<int> &list)  
{  
    for (list<int>::const_iterator iter = list.begin();
         iter != list.end();
         ++iter) {
        ...    
     }
}   

done

C++ Use of deleted function error

Reason:
contains a STD ::unique_ptr.
A std::unique_ptr cannot be copied
or
contains a std::shared_ptr
A std::unique_ptr cannot be copied
Solutions:
To:

//cpp1
class B{
A::getPTR(target_ptr)
}

//cpp2
class A{
    getPTR(const B& target_ptr)
    {
    std::shared_ptr<B> copied_ptr = std::make_shared<B>(target_ptr);
    }
}

target_ptr:is the ptr you want to copy.
Copied ptr: is the ptr you copied