c++ Getting non syntax use "&" to create a pointer [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have been working on my code and cannot seem to get this error fixed its the c_str': non-standard syntax; use '&' to create a pointer to member im not sure what im doing wrong.
HSAMPLE sample = BASS_SampleLoad(FALSE, fi.Recv_Song().c_str, 0, 0, 1, BASS_SAMPLE_MONO);
I get the error from the fi.Recv_song.c_str variable its suppose to be a string for a file location so the the function its in can play a mp3 file from that location. ps. the recv funtion is one i made in a header file.
string &Get_Song()
{
cin >> song; //this is the header file it is in.
mpc = path + song;
return mpc;
}
string Recv_Song()
{
return mpc;
}

Change line:
HSAMPLE sample = BASS_SampleLoad(FALSE, fi.Recv_Song().c_str, 0, 0, 1, BASS_SAMPLE_MONO);
to
HSAMPLE sample = BASS_SampleLoad(FALSE, fi.Recv_Song().c_str(), 0, 0, 1, BASS_SAMPLE_MONO);
c_str() is a function and you must call it as one.

Related

why can't I print the copied string individually? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
string s="1 23";
string a;
a[0]=s[2];a[1]=s[3];
cout<<a;
Here, I can't get output string a. But I can get all the individual elements by a[0].
Because a is empty, a[i] accesses it out of bounds for every possible i, causing undefined behavior.
Use a.push_back(s[i]) to add characters to a.
a is initialized as an empty string, so no memory is allocated for its characters, so when accessing it with [], you access unallocated memory, and that's undefined behavior.
One way to solve it is to create a as string with enough characters allocated. You can use the std::string fill constructor, that fills the string with a char of your choice:
std::string s = "1 23";
std::string a(s.size(), ' ');
This way you can put the characters in any index that exists in s.

Can we make array of sets in c++? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Like the following way:
set<int> s[3];
I have tried it but it gives error in the line where I had tried to access its elements by writing s[i][j] where the error says
no match for 'operator[ ]'
The problem is not the array of std::set-s but rather the way you try to access elements inside your set.
std::set doesn't support operator [], that is why you are getting the error:
no match for 'operator[ ]'
Instead, access object using find() in the following way:
auto iter = s[i].find(<value>);
if (iter != s[i].end()) {
[..] // Do something with iter
}
Elements of a set are not accessed by index. s[i] is the (i-1)'th set but s[i][j] doesn't mean anything. You can check whether an element is present in the set using the find function. For eg s[i].find(3)!=s[i].end() . You can loop through the elements in sorted order using for(int x : s[i]){} (C++11 and above) or using iterators.

operand types are incompatible ("bool (*)()" and "bool") [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to create a program in Visual Studio Community 2015 to play Tic-Tac-Toe. Sounds simple, not so much.
Anyway, I am using a function [bool playerCheck()] to make a switch for the function that checks whether the space that the user chooses is already taken.
void checkInput()
{
if (playerCheck == true)
Visual Studio gives the squiggly lines under the "==" and gives the message
operand types are incompatible ("bool (*)()" and "bool").
Not sure how the syntax should be for comparing function output to a constant.
playerCheck is a function which needs to be called in order to get the boolean result, i.e.:
if (playerCheck() == true)
bool (*)() is a type for a function which takes no arguments and returns a boolean.
playerCheck == true is an attempt to compare the function pointer with a boolean value, which leads to the compilation error which speaks for itself.
playerCheck() == true is calling the function and comparing the result (of type boolean) with the boolean value.
Note that if (x == true) can be generally abbreviated to if (x), so you could just write
if (playerCheck())

If statement c++ with text [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
My code:
{
int reply;
cout<<"Am I doing something wrong: ";
cin>> reply;
if-part( reply == "yes") {
cout<<"Good";
}
}
Hi guys, I am newbie, I googled and youtubed the stuff, but i just can not find an answer.
Why is this code not running well if you text in if condition, but if you put number, everything is fine?
Thank you.
In your case you are comparing an integer with a pointer (address)
if-part( reply == "yes") {
Reply is a in value.
"yes" is a c-string, so it is roughly equivalent to
const char *yes = "yes"
where yes would be a pointer to the first byte in the string in y_e_s -- for example, 0x75243
so you are comparing an integer value to a pointer value such as 0x75243 and they are unlikely to ever be equal.
In java script this would work (better) since it converts types for you -- C++ does not, at least not like this.
reply is of type int and you are comparing it as a string.That's not possible.
You can simply use:
if (reply == 1)
cout << "Good\n";

Why is my if statement skipping the else? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm writing a small program that count the lines of code. Here is the definition of a line of code:
- any line that contains code necessary for the program to function.
- blank line is not a line of code.
- comment is not a line of code.
- if there's code and a comment right after on the same line, it counts as well.
So I have this piece of code (simple if statement):
found = lineRead.find("/*");
if(found != string::npos)
{
found = lineRead.find("*/");
if(found != string::npos)
inComment = false;
else
inComment == true;
}
return inComment;
Assume that
String lineRead = "cout<<\"helloworld!\";/*blockcomment"
Bool inComment (is true if the previous line didn't have end block comment token)
So my program reaches the first if statement because it found /* in that line, looks for */, goes to the second if statement then jumps straight to the return statement without changing inComment (which is supposed to be set to true because the text on the next line is still inside the block comment).
Anyone know why that is?
Your problem is the infamous "double equal sign."
inComment == true;
In C++, == is used for compare, NOT for assignment. I think what you want is:
inComment = true;
Double equal sign is for comparison not assignment. Probably just a typo as I assume you know that. But yea, change == to just = :-)