I'm writing a program for a lab called Robot. When I compile it, I get this set of errors:
Robot.cpp:43:13: error: expected unqualified-id before 'double'
Robot.cpp:43:13: error: expected ')' before 'double'
The class is defined in the Robot.h header, and the code in the .cpp that's causing the problem looks like this:
/*
* Name: Robot (constructor)
*
* Creates the robot with the given maximum speed.
*/
Robot( double maxSpd )
{
MAX_SPEED = maxSpd;
traveled = 0 ;
elapsed = 0 ;
x = 0 ;
y = 0 ;
}
I can't figure out why the errors are there... and I also don't know what that error even means. How would I solve it?
I've managed to recreate your errors.
First of all, that semicolon after the signature has to go. It's declaring a function when you're trying to define one.
Robot(double maxSpd) {...} //no semicolon
SecondlyFirst of all, since this is outside of your class, you need to qualify it:
Robot::Robot(double maxSpd) {...}
Next, since MAX_SPEED is a constant, you need to put it in a member initializer:
Robot::Robot(double maxSpd)
: MAX_SPEED(maxSpd) {...}
Finally, you should move the rest of your assignments to the initializer list as well (not necessary, but good to do), keeping in mind that they should be listed in the order they are declared in the class definition:
Robot::Robot(double maxSpd)
: MAX_SPEED(maxSpd), traveled(0), elapsed(0), x(0), y(0) {
//body, which, from your example, would now be empty
}
The possible problem could be that there is a semicolon before the open bracket. But without seeing the whole class, it would be difficult to say so.
If this code isn't inside the class declaration, you could possibly be missing a "Robot::" namespace specification before the "Robot()" constructor name.
Example:
Robot::Robot(double maxSpd)
{
MAX_SPEED = maxSpd;
traveled = 0;
elapsed = 0;
x = 0;
y = 0;
}
Related
I am using C++ in Visual Studio Code. When I want to initialise a variable I cannot initialise it with {} (e.g., int x {0};). Instead, I have to use () (e.g., int x (0);).
The error I get when using {} is "error: expected ';' at end of declaration" Although I have put ; at the end of declaration.
I'm using clang 11.0.0 as the compiler. Is it related to the compiler?
The code runs through the terminal by ./filename command. However, it gives errors when running by coderunner extension in VSCode.
In order to initialize a variable with {} you must say it is = {}.
Like this:
int x = {3}; //you wouldn't really do this for simple variable though I don't think since you can just say int x = 3;
//or if you are making a custom object you might say:
MyObject object = {"apples", 3, "red"};
// to set the variables inside the object. in the order they are declared.
I hope this answers your question!
I am trying to use a merit function for my branching in Gecode. In the MPG, the Gecode Manual, an example merit function is stated, and how to pass it to the branching. But I cannot figure out where to put the function. Should it be inside the script or outside? Right now I have put it next to the copy function, etc. I cannot find any example code where someone uses a merit function.
I get the following error:
program.cpp(247): error C2059: syntax error: '}'
program.cpp(247): error C2853: 'm': a non-static data member cannot have a type that contains 'auto'
program.cpp(259): fatal error C1004: unexpected end-of-file found
This is the code I am trying out:
// ...
branch(*this, workers, BOOL_VAR_MERIT_MIN(m), BOOL_VAL_MAX());
}
auto m = [](const Space& home, BoolVar x, int i) {
return i;
}
// ...
I know it is stupid to make a merit function that just returns the index, I am just trying to make the simplest merit function to work before I do what I want to do.
According to the Gecode documentation the merit function should return a double. As suggested by the type definition of BoolBranchMerit:
typedef std::function<double(const Space& home, BoolVar x, int i)> Gecode::BoolBranchMerit
To be safe, you might also want to declare m as being an Gecode::BoolBranchMerit. So I think the following should fix your example:
// ...
branch(*this, workers, BOOL_VAR_MERIT_MIN(m), BOOL_VAL_MAX());
}
BoolBranchMerit m = [](const Space& home, BoolVar x, int i) -> double {
return (double) i;
}
// ...
This question already has answers here:
C/C++ changing the value of a const
(18 answers)
Closed 8 years ago.
I have a struct which is defined in types.h with the following code:
struct data_Variant {
FlightPlanSteeringDataRecord steeringData;
FlightPlanType flightPlan : 8;
MinitoteLegDataType legDataType : 8; // discriminent, either current or amplified
unsigned spare : 16;
union {
// currentLeg =>
CurrentLegDataRecord currentLegData;
// amplifiedLeg =>
AmplifiedLegDataRecord amplifiedLegData;
} u;
};
I am then trying to pass an instance of that struct as a parameter to a function in a C++ source file called dialogue.cpp:
void dialogue::update( const types::data_Variant& perfData){
...
}
I now want to change the value of some of the members of that struct inside this update() function. However, if I try doing this as I usually would, i.e.
perfData.etaValid = true;
I get a compile error which says: "C2166: l-value specifies const object". As I understand, this is because perfData has been declared as a constant variable. Am I correct in thinking this?
Since I didn't write this part of the code, but only want to use it to update the value displayed on the GUI, I don't really want to change the perfData variable by removing the const keyword, in case I break something else. Is there any way to change the value of a variable that has been declared as const?
I have tried declaring the same struct variable in another part of the code, without using the const keyword, to see if I can change the values of some of its members there... i.e. in Interface.cpp, I have added the following code to a function called sendData():
types::data_Variant& perfData;
perfData.steering.etaValid = true;
perfData.steering.ttgValid = true;
However, I now get the following compile errors on these lines:
error C2653: 'types' is not a class or namespace name
error C2065: data_Variant: undeclared identifier
error C2065: 'perfData': undeclared identifier
error C2228: left of '.steering' must have class/ struct/ union
Is there a way of updating the values of this struct? If so, how should I do it, and what am I doing wrong here?
I have added the following function to the dialogue.cpp source file, as suggested in the answer:
void dialogue::setFPTTGandETAValidityTrue(
FlightPlanMinitoteTypes::FlightPlanMinitoteData_Variant& perfData)
{
SESL_FUNCTION_BEGIN(setFPTTGandETAValidityTrue)
perfData.steeringData.fpETAValid = true;
perfData.steeringData.fpTTGValid = true;
SESL_FUNCTION_END()
}
You could add a wrapper for yourself.
void myupdate(dialogue& dia, types::data_Variant& perfData)
{
perfData.etaValid = true;
dia.update(perfData);
}
Then call myupdate() instead of dialogue::update().
You declare
void dialogue::update( const types::data_Variant& perfData){
...
}
that const is a declaration of you saying: "I won't modify the referenced object in this function". If you want to modify it in dialogue::update you have to remove the const keyword. Wrapping is not a solution, in my opinion, makes the code harder to maintain. Also I vote against remove const with const_cast.
The correct solution is to remove const from method declaration if you want to modify the referenced object inside that function.
Folks,
Problem Statement - Does C++ allow a (static) const be limited to a class scope, so I can get rid of #defines that pollute entire namespace?
My observation is NO (in the following DIFFERENT examples), and I'd like to find out why and what's the best alternative. VS generates error C2589: 'const' : illegal token on right side of '::'
EXAMPLE1
// a.h
class A
{
…
..
static const uint_32 myConst = 1234;
};
//b.cpp
include “a.h”
…
B()
{
uint32_t arr[A::myConst]; // C2589! const : illegal token on right side of '::'
}
EXAMPLE 2
// a.h
class A
{
…
..
enum blah
{
...
myConst = 1234,
..
};
};
//b.cpp
include “a.h”
...
B()
{
uint32_t arr[A::myConst]; // C2589! const : illegal token on right side of '::'
}
When you take your macro:
#define CONST 1234
and substitute it for where you use it:
static const int CONST = 1234;
The end result is nonsense:
static const int 1234 = 1234;
In another instance:
Int a1[a::CONST];
This also becomes nonsense:
Int a1[a::1234];
This all begs the question, what are you trying to do?
It looks like you're trying to create a member variable with the same name as your macro, CONST, here:
class A
{
static const int CONST = 1234;
};
However since when this code is compiled the macro has already been defined, the preprocessor changes this by substituting the macro before the compiler itself can get a crack at it. By the time the code is compiled, it looks like this:
class A
{
static const int 1234 = 1234;
};
Best is to just do away with the macro entirely, and then retrofit your code to use proper constants like you're trying to do here. Don't mix and match. At the very least, don't use the same name for the member as you do for the macro.
First of all your class is called A, as in capital A, not a. The class name is used to qualify the constant you are trying to use. So, change your code to use A::CONST. By the way, this is C++ not C# or Java, so there is no such thing as an Int, unless for some bizarre reason you decided to invent your own integer type.
As an aside, using all caps to name constants can collide with macros and is a good way to get into trouble, especially since pre-processing happens first and macros are substituted for all cases of the constants. This can often lead to invalid C++ code with syntax errors that are difficult to understand. That's why you should never use all caps to name constants, since this is a convention most commonly used for macros.
If I may make a guess, it looks like you're trying to use :: the same way you use . in Python.
It looks like you really don't understand what the scope resolution operator does, or how it works.
:: has a very specific, and quite limited usage. Until you understand it better, we're going to have a really hard time helping you.
I'm trying to do permutations with next_permutation from the stl, however I'm getting an error and I can't figure out how to fix it. I've tried googling, however the only results that come up are when people used the same function and function's variables name but thats not the case here.
Here's the error :
'__comp' cannot be used as a function
Here's the code :
struct rectangle{
int n;
int h;
int w;
};
bool check(const rectangle& rect1,const rectangle& rect2){
return rect1.n < rect2.n;
}
do{
//...
} while ( next_permutation(recs.begin(), recs.end(), check) ); // Getting error on this line.
Here's the full source code along with the sample input in case it's needed http://pastebin.com/eNRNCuTf
H = rec4.w + max(rec1.h, rec2.h, rec3.h);
You don't want to pass rec3.h there - The error message simply say that the 3rd argument to max can't be used as a function. I believe you intended:
H = rec4.w + max(max(rec1.h, rec2.h), rec3.h);