Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So, I've looked at other solutions with a similar problem to mine, but they all use loops to display the characters. What's required of me is to create 3 shapes using asterisks in C++. That's not so much the issue for me as actually getting them to display in the debugging window, instead of receiving a build error. Below is a test code of simply attempting to output the asterisk symbol:
#include<iostream>
using namespace std;
int main{
cout << "*" << endl
}
resulting in the following block of errors (file location redundancy omitted):
1>source.cpp(10): error C2440: 'initializing': cannot convert from'std::basic_ostream>' to 'int'
1> source.cpp(10): note: Reason: cannot convert from 'std::basic_ostream>' to 'int'
1> source.cpp(10): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>source.cpp(11): fatal error C1004: unexpected end-of-file found
I can only use programming based on what we've already learned, so I need to do these with nothing more than some basic cout statements.
main() is missing parentheses, and the statement is missing a semicolon.
int main() {
// ^^
cout << "*" << endl;
// ^
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
static_assert(0<decltype(AType::Id)::MaxIdLen);
using __type = char[decltype(AType::Id):: MaxIdLen + 10000];
[[maybe_unused]] __type aa = ""; //error initializer-string for char array is too long
I am getting this weird compiler error saying the initializer string is too long. But it is actually not. The first static_assert passed.
Has anyone seen such an issue before? I am using clang.
... compiler error saying the initializer string is too long. But it is actually not.
The compiler is not telling you that the string is too long to init aa. It's telling you that it's more data than it can handle, presumably because decltype(AType::Id)::MaxIdLen is a large value.
The compiler doesn't just store the string literal in the character array. It initialises the entire character array by using the string literal as a prefix, and padding the rest with zeros.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have this class namescores_t which is basically a data structure containing a first/last name linked to a score. While filling a vector of namescores_t objects, I check to see if the object already exists like so:
// reading from file and inserting data...
vector<namescores_t> NS;
namescores_t ns(names, scores); // initializing namescores_t object with names & scores read from file
if(find(NS.begin(), NS.end(), ns) == NS.end()); // if object does not exist in the list, add it
NS.push_back(ns);
However, I am getting an error no match for 'operator==' (operand types are 'namescores_t' and 'const namescores_t'). when calling find(NS.begin(), NS.end(), ns) == NS.end()
What am I doing wrong?
I was under the assumption that std::find() was able to use operator< to determine equivalency, similar to std::map, but this is apparently not the case. So I simply overloaded the operator== in order to fix the issue.
bool namescores_t::operator==(const namescores_t &ns) const
{
// returns whether both the names and scores of the object are equal to one another
return (scores.get_mean() == ns.scores.get_mean() && name == ns.name);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
FULL CODE
I am following this tutorial and have followed step by step from his tutorial and when he came to episode 8 I got an error in Level1.h.
void Level1::Load()
{
sprites = new SpriteSheet(L"test.png", gfx);
}
ERROR
'SpriteSheet::SpriteSheet(const SpriteSheet &)': cannot convert argument 1 from 'const wchar_t [9]' to 'wchar_t *'
I have written both Level1.h, Spritesheet.cpp and Spritesheet.h more than twice, tried to take away the "L" before the "test.png".
Also want to point out that I have Visual Studio 2017.
I would love you if you can solve my problem <3.
Full code
Literal strings in C++ are really constant arrays of the character type, that's why L"test.png" is mentioned as the typeconst wchar_t [9] (the size is 9 to fit the terminator).
As any other array it can decay to a pointer to its first element, and this pointer have the type const wchar_t*.
Note the use of const in the types above... That's what's missing in your constructor argument. It needs to be
SpriteSheet(const wchar_t* filename, Graphics*gfx);
// ^^^^^
// Note the const here
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Why does List MakeEmpty(List L) have to be written in a form like:
List
MakeEmpty(List L)
in Mark Allens Book?
This is just a matter of styling preference; the command here is the same regardless of whether you write:
List
MakeEmpty(List L)
or
List MakeEmpty(List L)
It really doesn't make a difference besides changing the readability of the code. The author of the book you mentioned may find adding some whitespace more readable compared to the conventional method you compared it to.
One way you can differentiate between whitespace and a real compiling difference is the semicolon. Wherever there is a semicolon, the line terminates. In your case, if there was:
List; // Notice semicolon here
MakeEmpty(List L)
Then this would have changed the code (the syntax is wrong here as it changes the original meaning of the code, but I just wanted to make my point with this example).
Adding whitespace makes the code more readable to the programmer; it adds to the style, especially if it is being presented to a larger audience. The amount of whitespace preferred varies from person to person.
I tried to make the simplest example possible here: the main() function. See the 2 ways I wrote it:
Method 1:
int // Return type of function on different line from function name and arguments for function declaration
main(void)
{
cout << "hello" << endl;
return 0;
}
Method 2:
int main(void) // Return type of function on same line as function name and arguments for function declaration
{
cout << "hello" << endl;
return 0;
}
Just like your function declaration, here, the return value data type's position is changed between the 2 samples of code. However, the output is the same: It outputs hello followed by an endline.
Hope this helps. If I've made a mistake anywhere, or you have further questions, ask me in the comments box.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The error message appears on Xcode that says "invalid operands to binary expression.
in my code I'm using an array of a struct, i'm trying to sort input data in an ascending order, and i'm getting this error message at the "if" condition shown in the print screen at this link:
https://www.dropbox.com/s/0mch2gbxcif0a20/Screen%20Shot%202016-04-27%20at%2012.45.45%20PM.png?dl=0
The Code
if (studentsInfo[i] > studentsInfo[i + 1]) {}
The Error
Invalid operands to binary expression ('students' and 'students')
What do you compare in your program? As I see, you have to compare names, but all you do is compare an array element which is a struct data type.
If you are trying to compare names, you have to use dot "." operator to reach names. After yo compare names, you can change the elements's place.
The error means that > only takes two arguments and you are using it for something else. In this case you are comparing an entire data structure that does not have an override for > operator and is an undefined behavior. StudentsInfo[i] is a data structure that has more than one element in it. Replace the StudentsInfo[i] with StudentsInfo[i].GPA or another element whose data type has a defined > operator.