Relative Content

Tag Archive for c++hashblockchain

class “std::basic_string<char, std::char_traits, std::allocator>” has no member “c_str”C/C++(135)

As a personal project I was thinking of creating my own blockchain to use in a cryptocurrency that I also would like to create entirely from scratch in C++, but while I was writing my code I suddenly came across an inexplicable error, and actually multiple errors. Currently my project is still in its infancy, and in fact I only have *block.h* and *block.cpp* files, which should manage each block of my cryptocurrency blockchain. The block.h code is all correct, but the **block.cpp** code is full of errors, for which I have not been able to find the solution online. My VS Code says that there are 18 errors in the project. How do I fix all these errors? Specifically, my errors are the following * class “std::basic_string<char, std::char_traits<char>, std::allocator<char>>” has no member “c_str” class “std::basic_string<char, std::char_traits<char>, std::allocator<char>>” has no member “size” namespace “std” has no member “hex” namespace “std” has no member “setw” namespace “std” has no member “setfill” no instance of overloaded function “Block::Block” matches the specified type expected a ‘{‘ identifier “hash” is undefined class “Block” has no member “getHash” identifier “hash” is undefined class “Block” has no member “getPrevHash” identifier “prevHash” is undefined declaration is incompatible with “<error-type> Block::getData()” (declared at line 29 of “C:UserslauraOneDriveDesktopFrancescoC++Francoinblock.h”) class “Block” has no member “getTimestamp” identifier “timestamp” is undefined declaration is incompatible with “<error-type> Block::calculateHash() const” (declared at line 41 of “C:UserslauraOneDriveDesktopFrancescoC++Francoinblock.h”) no operator “<<” matches these operands identifier “prevHash” is undefined* Here is also the code of my block.cpp and block.h files block.h: // This file contains the code that defines each block #ifndef BLOCK #define BLOCK #include <iostream> // To manage inputs and outputs #include <string> // To manage strings #include <ctime> // To have the timestamp #include <vector> // To manage collections of data using namespace std; // Means that we can use names for objects and variables from the standard library class Block{ public: // Accessible from anywhere and from anything in the program // Constructor: like the _init_ functio in a Python class Block( int index, // This is the index of the block in the blockchain. It is used to identify the position of the block within the blockchain. const string &data, // Constant string variable (in the blockchain nothing can be modified) which contains all the data relating to a possible transaction. I use ‘&data’ instead of ‘data’ to call the original value of date and avoid manipulations in the blockchain following transactions. const string &prevHash // This is a reference to the hash of the previous block, which thanks to ‘const’ cannot be changed, thus creating a chain of blocks. ); // Public functions int getIndex(); // To get the index of the block time_t getTimeStamp(); // To get the time stamp of the block string getCurrentHash(); // To get the hash of the current block string getPrevBlockHash(); // To get the hash of the previous block string getData(); // To get some other data of the block private: // Can only be accessed by the functions inside the class // Private variables: used to store properties of the block int index; time_t timestamp; string data; string prevBlockHash; string currentHash; // Private function string calculateHash() const; // A function to calculate the Hash of the block }; #endif // BLOCK block.cpp: #include “block.h” #include <sstream> #include <openssl/sha.h> std::string sha256(const std::string str) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, str.c_str(), str.size()); SHA256_Final(hash, &sha256); std::stringstream ss; for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) { ss << std::hex << std::setw(2) << std::setfill(‘0’) << (int)hash[i]; } return ss.str(); } Block::Block(int idx, const std::string &data, const std::string &prevHash) : index(idx), data(data), prevHash(prevHash), timestamp(std::time(0)) { hash = calculateHash(); } std::string Block::getHash() { return hash; } std::string Block::getPrevHash() { return prevHash; } int Block::getIndex() { return index; } std::string Block::getData() { return data; } time_t Block::getTimestamp() { return timestamp; } std::string Block::calculateHash() const { std::stringstream ss; ss << index << timestamp << data << prevHash; return sha256(ss.str()); } Thank you very much ” but there is a message Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon. why?