Cryptonote C++ compile error with invalid operands - c++

I am working with cryptonote repo for a project and am at the point where I need to compile the binaries.
When I run make, I get the following error:
/Documents/huntcoin/src/CryptoNoteCore/SwappedMap.h:185:14: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘const char [24]’ to binary ‘operator<<’
std::count << "SwappedMap cache hits: " << m_cacheHits << ", misses: " << m_cacheMisses << " (" << std::fixed << std::setprecision(2) << static_cast<double>(m_cacheMisses) / (m_cacheHits + m_cacheMisses) * 100 << "%)" << std::endl;
~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
I am not super familiar with C++ and am sure it might be a simple parenthesis error, but it could be something more.
For some context, the previous make error I got was that std::cout was not defined, which I assumed was just a typo for count. Maybe that was wrong as well.
Any help with C++ or cryptonote would be much appreciated!

You've got an extra n that is causing you trouble. The code should read:
std::cout << "SwappedMap c.....
std::cout is the default console output (console output) stream while std::count is not defined
The std::cout is defined in a header file iostream so all you need to do is put this line of code next to other #include statements at the top of your file:
#include <iostream>
Cheers

Related

Macro for push_back giving problems

I am starting to code in c++.
I was learning macros when this happenned:
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
int main() {
vector<int> hello;
hello.PB(3);
hello.PB(2);
cout << hello < "\n";
}
My compiler shows, pointing to line 3:
Error: statement cannot resolve address of overloaded function
For your code I get problems with the < instead of << and what I assume the main problem:
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<int>’)
cout << hello << "\n";
It is telling you that there is no known way to output a whole vector to cout.
The simple way to fix that is
cout << hello[0] << " " << hello[1] << "\n";
This gets you an output of
3 2
The more complex way, with more convenient result, is to do the overloading yourself accordingly.

Clang AST find only syntax errors

I'm using Clang to create some internal static code analyzers. For one of the analyzers, we need to take a raw string and check if it has any syntax errors.
We shouldn't consider missing symbols, missing headers, invalid function calls etc. as invalid syntax - as the only meaning is to see if it's a valid C/C++ code or not.
I thought initially that I could do it with ASTUnit:
auto AST = tooling::buildASTFromCodeWithArgs(MyCode,
Args,
"input.cc",
"clang-tool",
std::make_shared<PCHContainerOperations>(),
tooling::getClangStripDependencyFileAdjuster(),
tooling::FileContentMappings(),
&DiagConsumer);
llvm::outs() << "hasUncompilableErrorOccurred " << AST->getDiagnostics().hasUncompilableErrorOccurred() << "\n";
llvm::outs() << "hasUnrecoverableErrorOccurred " << AST->getDiagnostics().hasUnrecoverableErrorOccurred() << "\n";
llvm::outs() << "hasErrorOccurred " << AST->getDiagnostics().hasErrorOccurred() << "\n";
Taking two inputs: Hello world and #include <undefined.h> - both yields 1 in the outputs above - even when #include <undefined.h> is a correct C statement, but the issue with it (unlike with hello world, which's not a valid C code) - is that undefined.h is missing. Similarly, taking: int* p = malloc(sizeof(int)); as code will yield error in all of these calls if stdlib.h wasn't included.
I try to avoid such errors, so that every case, except from hello world, will be considered as valid code.
I did tried to iterate over it by creating a Raw Lexer, but it won't give me sufficient information.
Lexer Lex(CharRange.getBegin(), PP->getLangOpts(), Text.data(),
Text.data(), Text.data() + Text.size());
Token RawTok;
do {
Lex.LexFromRawLexer(RawTok);
llvm::outs() << "\t- " << RawTok.getKind() << "\n";
} while (RawTok.isNot(tok::eof));
I'd love to get any suggestions!

What is "expected unqualified-id" error in C++?

Im trying to learn stl:bitmap, but I'm getting the following error:
Headers added
- bitset
- string
Ive searched other SO posts for this error, but they are not related to bitset.
My Code
int main(){
bitset<size> bs;
bitset<10> bs2(45);
bitset<13> bs3("1100101");
cout << "bs: " << bs << endl;
cout << "bs1: " << bs2 << endl;
cout << "bs2: " << bs3 << endl;
cout << endl;
cout << "bs has " << bs.count() << " set bits" << endl;
cout << bs.size() << endl;
cout << bs2.size() << endl;
cout << bs3.size() << endl;
}
My Error: Error in the last 3 cout statements.
$ g++ test.cpp
test.cpp:28:16: error: expected unqualified-id
cout << bs.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
test.cpp:29:17: error: expected unqualified-id
cout << bs2.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
test.cpp:30:17: error: expected unqualified-id
cout << bs3.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
3 errors generated.
$
Remove the #define size 16 from your program. I guess you've written this line at the top of your program.
size macro that you'defined conflicts with size() member function. Use const variable instead of macros. You should use const int size=16;
You seem to have a macro defined in test.cpp line 6 which is string replacing your attempt to call the function size.
Your line is actually saying:
cout << bs.16() << endl;
cout << bs2.16() << endl;
cout << bs3.16() << endl;
If you want to use macro's it's good practice to make them as descriptive as possible and use ALL_UPPER_CASE to avoid these type of issues.
e.g.
#define BITSET_DEFAULT_SIZE 16
The error descriptions given to you by the compiler are very descriptive and let you know that a macro is the reason for this problem:
test.cpp:28:16: error: expected unqualified-id
cout << bs.size() << endl; <- this is telling you the starting position of the error
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16 <- this is telling you a macro is involved, and giving its value
Also, it's not good practice to use using namespace std in your programs due to std containing so many generic named functions. For example, if you create a function called size, you've suddenly overwritten std::size.
Here is a good post pointing out why this is a bad idea
Use
#undef size
immediately after the line
bitset<size> bs;
This will hide your macro and the rest of the code should now compile.
NOTE: This is not a permanent fix. But in case the macro is in a header file which is included in many files, this will give a temporary fix. But using const in C++ is recommended over a macro.

Outputting cerr using cout

I came across a piece of code that does basically the following:
#include <iostream>
using namespace std;
int main()
{
cout << cerr << " Hi.";
return 0;
}
Output:
0x601088 Hi.
First of all, why would anyone do 'cout << cerr' it does not make sense.
Second of all, what is the meaning of the output above?
Worth to mention that on my machine the above code compiles and executes without errors.
However a much more complex code (doing the same thing as above) on a different machine (server ssh connection) running the same version of gcc 5.4.0, produces this error when doing make (shortened for clarity):
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::ostream {aka std::basic_ostream<char>}’)
cout << cerr << "DB: Field " + e.table + "[" + e.index + "]." + e.field
Any thoughts on this?
Until c++11, std::basic_ios offered an implicit conversion to void*. This code won't compile with c++11 or later. You basically have this, which compiles with older versions of gcc :
#include <iostream>
int main()
{
void * x = std::cerr;
std::cout << x << " Hi.";
return 0;
}

C++ substr method - "invalid use of ‘this’ in non-member function"

I tried to compile the following code
std::string key = "DISTRIB_DESCRIPTION=";
std::cout << "last five characters: " << key.substr(this.end()-5) << '\n';
And the compiler says
error: invalid use of ‘this’ in non-member function
std::cout << "last five characters: " << key.substr(this.end()-5) << '\n';
^
substr is a "public member function" of std::string, why can't I use this?
I know I could just reference key again instead of this, but my original code was
std::cout << "Description: " << line.substr(found+key.length()+1).substr(this.begin(),this.length()-1) << '\n';
In the second use of substr, the string does not have a name, so the only way to refer to it would be this. I fixed it with
std::cout << "Description: " << line.substr(found+key.length()+1,line.length()-found-key.length()-2) << '\n';
But I am now curious to why this won't work.
this is only available when you are writing code as part of a non-static method of a class. In your particular case, it seems obvious to you that this should refer to key, but the compiler sees no reason for that.
Also, string.substr() takes an integer indicating the beginning position. string.end() returns an iterator, which will not work. What you likely want to do here is call string.length().
Simply replace the first piece of code with:
std::cout << "last five characters: " << key.substr(key.length()-5) << '\n';
And you should be okay.