What is "expected unqualified-id" error in C++? - 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.

Related

How do I make all class member functions write to the same file in C++?

ofstream outPatron;
outPatron.open("patronRec.txt")
I am trying to make it so that all of my different functions within the class Patron can use this ofstream to write to a file which will contain all of the data for each patron.
I have tried putting this code in the class definition, which leads to an error as such things can only be declared within functions. (or so I assume, considering that that's the only way I got it to work) I tried putting it inside a function definition within the class definition, and every time that function ran, it erased the entire file and started again with only the newest iteration.
Finally, I tried putting this in main, but it wouldn't even compile, saying that each time I use outPatron in the class functions, it's an undeclared identifier.
This is the function I need to use it in.
void Patron::AddPatron(string a, int b, int c, int d) {
SetName(a);
SetID(b);
SetFineBalance(c);
SetBooksOut(d);
count++;
outPatron << "Name: " << Patron::GetName() << endl;
outPatron << "ID: " << Patron::GetID() << endl;
outPatron << "Fine Balance: " << Patron::GetFineBalance() << endl;
outPatron << "Books Out: " << Patron::GetBooksOut() << endl << "-------------------" << endl;
}

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.

Error in function that returns 3 values

I am using a function that returns 3 values using tuples in C++17.
The program is
void test_noisy()
{
s4prc::Noisy<s4prc::StrVec> big{s4prc::makeStrVec(10'000)};
std::cout << ">>> big=" << big << '\n';
s4prc::Noisy<std::string> evenStr;
s4prc::Noisy<std::string> oddStr;
int totalLength;
auto [evenStr, oddStr, totalLength]=s4prc::manyResults(big);
}
When I compile, I obtain this error :
error: expected unqualified-id before ‘[’ token
auto [evenStr, oddStr, totalLength]=s4prc::manyResults(big);
^
GNUmakefile:192 : la recette pour la cible « prog.o » a échouée
Thanks,
Younès
Not sure it's the only problem but... with
s4prc::Noisy<std::string> evenStr;
s4prc::Noisy<std::string> oddStr;
int totalLength;
auto [evenStr, oddStr, totalLength]=s4prc::manyResults(big);
you're defining and redefining evenStr, oddStr and totalLength. This should give a compilation error.
Try deleting the first definitions (the three rows before auto).
Second: are you sure you're compiling C++17?
The answer that I find is to use make_tuple in the return of the definition of the function;
return std::make_tuple(std::move(even), // std::move() prevents from
std::move(odd), // copying existing locals
std::move(length)); // to tupl
Besides, when calling the function use :
auto data=s4prc::manyResults(big);
and get the three compounents of data with :
std::cout << ">>> evenStr=" << std::get<0>(data) << '\n';
std::cout << ">>> oddStr=" << std::get<1>(data) << '\n';
std::cout << ">>> totalLength=" << std::get<2>(data) << '\n';

Cryptonote C++ compile error with invalid operands

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

How do I correct "expected primary-expression before...?"

I'm just beginning to program and I have no idea what I'm doing. My professor gave us Program Sets to do and I've completed it, but when I Compile the file I get
" J:\Untitled1.cpp In function `int main()':
"36 J:\Untitled1.cpp expected primary-expression before '<<' token "
Here's the full set, remember now that I'm a beginner:
/** CONCEPTS PROGRAM #1, TEMPLATE
PROGRAM Name: Yay.cpp
Program/assignment:
Description: Finds total
Input(s):
Output(s):
suffering_with_c++
Date of completion
*/
//included libraries
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h.>
#define cls system("cls")
#define pauseOutput system("pause")//
using namespace std;
int main()
{
//variable declaration/initialization
time_t nowIsTheMoment;
time(&nowIsTheMoment);
string dateTime;//
cls;
cout <<"\new live in the moment--only this moment is ours. The Current Date and time is: "
<<ctime (&nowIsTheMoment); << endl;//
cout << "\nMy name is Moe Joe." <<endl;//
cout << endl << "I think Computer Programming with C++ will be a bit more PHUN now!"
<< endl;
dateTime = ctime(&nowIsTheMoment);//
cout << endl << "\nYo ho! I am here now...\n" << endl;
cout << endl << "The Current Date and time is: "
<<dateTime <<endl;//
cout << "\nI know clearly that, if I DO NOT comment my programs/project work thorougly, I will lose substantial points.\n" ;
cout << "\bHere is Pause Output in action....\n" << endl;//
pauseOutput; //
cls;//
return 0;
}
Remove the semicolon on line 36
<<ctime (&nowIsTheMoment); << endl;
^
|
You forgot to #include <string> and qualify string and cout with std::.
Start by removing the . after <time.h> it should probably help. Then you got this
ctime (&nowIsTheMoment); << endl;
which can't compile because << needs a left operand (ie: remove the semi-colon).
I don't mean to be rude, but you should try a little bit harder before asking questions on StackOverflow...