invalid expression, assumed zero must be a constant expression - pawn

(1204) : error 029: invalid expression, assumed zero
(1204 -- 1205) : error 008: must be a constant expression; assumed zero
(1203) "{CCFF99}[INFO]:{FFFFFF} Nu uita sa faci poza la {CCFF99}/stats{FFFFFF} periodic.",
(1204) }
(1205) new rCheckList[sizeof(rMessageList)];
Please help me idk what to do :c,this is in pawn..

solved it had to put }; and remove the , from 1203

Related

Is it possible to use the ternary operator "?" to fill an array list in C/C++?

This is probably a dumb questions. I'm modifying a code developed by someone else. I need to particularize the value of some chars array based on a logic variable ThreeDim. I'm trying to do this without success.
int VarNumber = ThreeDim==1 ? 3 : 2;
const char* VarList [] = ThreeDim==1 ? {"X","Y","Z"} : {"X","Y"};
But the compiler is giving me errors like
error: expected ‘;’ before ‘}’ token
error: initializer fails to determine size of ‘VarList’
VarList needs to be a const char* due to downstream requirements. And its size should be VarNumber. Thanks
You might consider using the preprocessor, #define THREE_DIM, and then use #ifdef to select one or the other code to compile:
#define THREE_DIM
#ifdef THREE_DIM
int VarNumber = 3;
const char* VarList [] = {"X","Y","Z"};
#else
int VarNumber = 2;
const char* VarList [] = {"X","Y"};
#endif
I don't think you can do this with different sized array initializers. However, you can put a conditional expression in the initializer:
const char* VarList [] = {"X", "Y", ThreeDim == 1 ? "Z" : nullptr};
This will always give you a 3 element array with the last element either "Z" or a null pointer.
No, because e.g. {"X","Y","Z"} is not an expression. It's handled specially by the compiler for variable definitions.
And if you're programming C++ you should be using std::vector and std::string instead, then it would be very simple (but verbose):
std::vector<std::string> VarList = ThreeDim==1 ?
std::vector<std::string>{{"X","Y","Z"}} :
std::vector<std::string>{{"X","Y"}};
As per C11 6.7.9, arrays aren't initialized with an expression but with an initializer list; these are two different syntactical elements and can't be mixed. This derives from the fact that arrays aren't assignable, and treating one as a value (as opposed to a value that decays to a pointer) therefore doesn't make sense. Since they aren't values, there's no way to create a legal expression that passes around whole arrays.
The closest direct analogue to what you're asking for would be:
const char** VarList = ThreeDim==1 ? (const char*[]){"X","Y","Z"} : (const char*[]){"X","Y"};
...but this comes at the cost of changing the type of VarList, and is probably not what you want.

Error 23 error C2440: 'return' : cannot convert from 'char [128]' to 'char (&&)[128]'

Basically, I wrote the signature of a method that returns a vector of char and I am getting this error
Error 23 error C2440: 'return' : cannot convert from 'char [128]' to 'char (&&)[128]'
Here is the signature of the method
vector<char[256]>returnMessage(string ACK)
{
}
Also, I can't declare a vector of char as a global variable.
When I do this:
std::vector<char[256]> myvector;
I get:
Error 24 error C1903: unable to recover from previous error(s); stopping compilation
and
Error 23 error C2075: 'Target of operator new()' : array initialization needs curly braces
You have a problem with instantiation, but not declaring type. Just use:
struct Chars256 {
char arr[256];
};
And you will be able to:
vector<Chars256> returnMessage(string ACK) {
Chars256 arr;
Chars256 arr2 = {{0}}; // setting value only for first element, all other will be zeros!
// ...
}
You receive such errors because of internal code of vector. Template substitution fails.
First error is connected to move semantics. Vector tries to move char[256] and fails.
Second with new means that somewhere in code of vector there is new T() and when T is plain array type substitution fails again.
Solution is to use char*

Ternary expression which "does nothing" (noop) if the condition is false?

Out of curiosity I started wondering if it's possible to have a ternary expression that, if it evaluates to false, does nothing in the false branch.
Ie is there a way to write something like this:
variable = (someBool) ? i : <do nothing>;
As opposed to:
if (someBool) {
variable = i;
}
I tried ((void)0) or while(false){}; as no-op but the compiler expects an expression.
UPDATE:
I realized the question lost some meaning because I tried to make the code easier. The initial idea I had was to initialize a static var with a ternary - using the static var itself as the condition:
static int var = (var != 0) ? var = 1 : (var already initialized, do nothing);
This is assuming that uninitialized variables are initialized to 0 which is not always true (or never in release builds, not quite sure). So maybe it's a hypothetical question.
how about short-circuit?
int variable = 0;
bool cond = true; // or false
(cond && (variable = 42));
printf("%d\n", variable);
How about this:
variable = (someBool) ? i : variable ;
Though I would personally prefer the original if statement
Compilers not only expect expression, but the expression the returns type on the left side (the type of variable whatever is it). So, no you can not do that. It's not conditional execution, but variable member assignment.
These are completely different things.
In second example :
if (someBool) {
variable = i;
}
you do not assign anything, but simply execute based on condition. So in your case, where you don't want to do anything (not assign anything), the way to go is conditional execution so use simply the second case.
The format of the conditional expression is
<expression> ? <expression> : <expression>
In other words, it must have some expression.
Addressing your edit: in C99 variables of static scope are initialised to 0. However, I have never really trusted that because I've been programming in C since the K&R days.
Anyway, just initialise the variable. As the variable is static, it's only going to happen once during the whole execution time of the program.
You could do:
variable = !someBool ?: i;
Since the ?: will no-op when the if expression is true but assign i if it's false.
Note: This has only been tested in Obj-C
How about
(someBool) ? (variable = i) : NULL;
For C# says:
Syntax:
condition ? first_expression : second_expression;
And it says about first_expression and second_expression:
Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.
If you were to evaluate a nullable object type instead of bool, you could always write:
variable = myVar ?? i;
Hacky/cludgey/impractical - probably all 3, but for the sake of this question it's a way of omitting an 'else'.
Try: null lambda.
auto null_lambda = [](){return;};
int a = 1;
int b = 2;
vector<int> c;
a > c ? b = c.push_back(b) : null_lambda();

Proper casting of the object in C++, value field is not changing

I have a class:
class Para{
public:
int wrt, liczbaWystapien;
Para(){}
Para(int wrt, int liczbaWystapien){
this->wrt = wrt;
this->liczbaWystapien = liczbaWystapien;
}
Then there is other template class, and I do not know how to cast object to Para, becuase first way does not affect field value at all.
else if (is_same<T, Para>::value){
//dynamic_cast<Node<Para>*>(node)->key.wrt++;//this way no error occured but value of field **wrt** stays the same
node->key.wrt++;//error below
Error 4 error C2039: 'wrt' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
EDIT:
Node* paraNode = static_cast*>(node);
Para para = paraNode->key;
para.wrt = para.wrt + 1;
That gives
Error
4 error C2440: 'static_cast' : cannot convert from 'Node *' to 'Node *'
Something seems weird, because key's type supposed to be Para. What happens if you spell everything out?
Node<Para>* paraNode = dynamic_cast<Node<Para>*>(node);
Para para = paraNode->key;
key.wrt = key.wrt + 1;
Other suggestions: if fore some reason you are sure about the template type, you can use static_cast<> (or reinterpret_cast<>), it's faster than dynamic_cast<>, which really discovers and check the type hierarchy tree.
If you do
Para para = paraNode->key;
you get a copy of the key. Then you increment wrt. If you do a printf following that line, you will probably get the expected value. However if you call this from inside a function then the original paraNode will not be modified.
You need to store key as Para* key then access key.wrt with key->wrt++ then after the function you will get the expected value. I suggest you read on how stack and heap variables work as well as copy constructor in c++

Getting error: expression in new-declarator must have integral or enumeration type C++

I don't understand why I'm getting this error. I used the same code previously and it gave me no problem, but now for some reason im getting an error.
outputf3 << "The sorted list is as follows: \n\n";
sorting = new float [function.sort()]; // Here is where the error is.
for (i=0; i < length ; i ++)
{
outputf2 << "\n" << sorting[i] ;
}
For some reason it's saying
error: expression in new-declarator must have integral or enumeration type
But i used the same thing here in a previous part of the program.
inputs = new float[length];
inputf.clear();
inputf.seekg(0, std::ios_base::beg);
I have no idea why it's doing this at all.
What I'm doing is reading random numbers in from a file and then sorting them in another part of the function and writeing them out to the output file.
Why do you think:
inputs = new float[length];
is the same as
sorting = new float [function.sort()];
?
Unless function is a class instance and sort returns an integral type, it's certain it won't work.
The statement:
Type* t = new Type[x];
dynamically allocates x objects of type Type. It's fairly obvious why x has to be of integral type.
EDIT: As per your comment:
The size to be allocated needs to be of integral type, but you return a float. But even then, it wouldn't make sense. You need to return the size of inputs.
i'm gonna go out on a limb and say function.sort() returns void. meaning this line:
sorting = new float [function.sort()];
is the same as writing...
sorting = new float [void];
which doesn't make any sense. you probably want the length of whatever function.sort() is working on?