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
In my header file I declare structure
typedef struct _PreprocessedImage
{
cv::Rect straight;
cv::Rect diagonal;
bool empty = true;
...
...
} PreprocessedImage;
Then I create class with method
std::vector<float> processData(cv::Mat &image, bool drawRegions = false, PreprocessedImage &preproc);
.
Try to compile and got
"error: default argument missing for parameter 3"
But when I try to declare method with default value, like that:
std::vector<float> processData(cv::Mat &image, bool drawRegions = false, PreprocessedImage &preproc = PreprocessedImage());
.
I got
"error: invalid initialization of non-const reference of type
'PreprocessedImage& {aka _PreprocessedImage&}' from an rvalue of type
'PreprocessedImage {aka _PreprocessedImage}'"
How can i fix it?
All parameters with defaults should be at the end of the list, so you need something like:
std::vector<float> processData(cv::Mat &image, PreprocessedImage &preproc, bool drawRegions = false);
.
To add to paxdiablo's answer.
Yes, the default-argument parameters must come last. Your attempt to work around this by also giving preproc a default argument failed because a temporary cannot bind to an lvalue reference (it would have to be const); besides, giving something a default "for the sake of it" is probably not what you wanted to do.
An alternative, that doesn't require re-arranging your existing function, is to write a forwarding overload instead of using default arguments:
std::vector<float> processData(cv::Mat& image, bool drawRegions, PreprocessedImage& preproc)
{
/* ... */
}
std::vector<float> processData(cv::Mat& image, PreprocessedImage& preproc)
{
return processData(image, false, preproc);
}
By the way, you don't need (or want) that antique C-style typedef struct A { ... } B syntax in C++ (unless you require direct C compatibility); you just want struct B. And, if you really must go for the former, you should pick a name that isn't reserved to the implementation.
Related
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 months ago.
Improve this question
So I want to initialize a variable with a value like this:
ADM_job_t job{42};
The structure of ADM_job_t is this one:
typedef struct adm_job* ADM_job_t;
And adm_job looks like this:
struct adm_job {
uint64_t id;
};
So my idea was to intialize my variable with an int, because is the "final" type, but I'm getting an error saying that I can not initialize an adm_job with an int. I can not change the structure that has been provided. How can I do it?
Thank you!
As mentioned in the comments you do actually have to create a adm_job object.
Here is one way
adm_job obj{42};
ADM_job_t job = &obj;
Even if you can't change the adm_job structure, you can probably still ignore ADM_job_t for object creation, and use smart pointers instead. I recommend starting with something like this:
#include <memory>
struct adm_job {
uint64_t id;
};
typedef struct adm_job* ADM_job_t;
void foo(ADM_job_t p) {
}
int main () {
//auto job = std::make_unique<adm_job>(42); // C++20
auto job = std::unique_ptr<adm_job>{new adm_job{42}}; // C++11
foo(job.get());
}
job's type is std::unique_ptr<adm_job>. As you can see, you can still pass the result of job.get() to any old C function that makes non-owning use of ADM_job_t.
You needn't worry about deleting the object like this.
Read about unique_ptr. There are some other types of smart pointer depending on who else is going to "own" the pointed two object during its lifetime.
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 3 years ago.
Improve this question
im making an operator for comparing objects of my own class 'Paciente', but when calling (const) getters of that class, Im getting errors.
Here I leave the code:
bool operator==(const Paciente& p1, const Paciente& p2){
return (p1.getNombre() == p2.getNombre() && p1.getApellidos() == p2.getApellidos());
}
Here the class Paciente:
class Paciente {
private:
string nombre_;
string apellidos_;
int edad_;
public:
Paciente(const string &nombre="none", const string &apellidos="none", const int &edad=0): nombre_(nombre), apellidos_(apellidos), edad_(edad){};
const string & getNombre(){return nombre_;};
const string & getApellidos(){return apellidos_;};
const int & getEdad() {return edad_;};
string setNombre(const string &nombre){nombre_ = nombre;};
string setApellidos(const string & apellidos){apellidos_ = apellidos;};
int setEdad(const int &edad){edad_ = edad;};
};
Class Paciente is allocated at 'paciente.hpp', and the operator and many more functions at 'functions.hpp'. I know it's not the right way to implemente operators, but with the other ways also got errors. Thanks.
EDIT:
forgot to mention, the error is: passing ‘const Paciente’ as ‘this’ argument discards qualifiers [-fpermissive]
bool operator==(const Paciente& p1, const Paciente& p2)
This has two constant objects as parameters.
However,
const string & getNombre(){return nombre_;};
is not a constant method and thus not allowed to be called on a constant object, which is what you try to do with p1.getNombre().
Simply change it to
const string& getNombre() const { return nombre_; }.
To elaborate a bit more on that, since you wrote "but when calling (const) getters of that class": A method is exactly const if you declare it as such, as I did. Simply it semantically not changing the object is not enough. The compiler is not "clever" enough to check semantics, and it really shouldn't be, easy source for errors.
Also note that it returning a const string& does not make the method const. Simply think of a signature like
const string& iterate_one_step_and_return_status_string();
(yes, this is horrible long name one should not use).
By the way, the semicolons at the end of your method implementations do nothing. You need those only when you only declare but not implement.
Also, I'd recommend to use english for everything but values. In this case, it is not that important, but should you post code for which us understanding what you want to do is important, it is helpful if we can get to know that by the names.
Edit: another problem with your code I noticed:
string setNombre(const string &nombre){nombre_ = nombre;};
This method has return type string yet does not return anything. I actually wonder why your compiler hasn't given you a warning or error like Error: control may reach end of non-void function.
(This also applies to your other setters.)
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
I read on another stackoverflow post (variable length array error when passing array using template deduction) that the following should be possible:
#include <iostream>
int input() {
int a;
std::cin>>a;
return a;
}
int main()
{
const int b = input();
int sum[b];
std::begin(sum);
}
Except that it doesn't seem to work, I still get an similar error.
In function 'int main()':
16:17: error: no matching function for call to 'begin(int [b])'
16:17: note: candidates are:
Followed by information on possible templates it could fit.
You can use std::begin(sum) only when sum is regular array, not when it is a variable length array.
The following is OK.
const int b = 10;
int sum[b];
std::begin(sum);
In your case, b is not known at compile time. For arrays whose length are not known at compile time, it's better to use std::vector instead of relying on a compiler specific extension. The following is OK.
const int b = input(); // You can use int b, i.e. without the const, also.
std::vector<int> sum(b);
std::begin(sum);
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
Soo I got this tiny piece of code right here:
#include "stdafx.h"
#include <iostream>
using namespace std;
void WhyDoesThisNotWork();
int main()
{
void WhyDoesThisNotWork();
return 0;
}
void WhyDoesThisNotWork()
{
cout << "Why don't you just print this for god's sake?" << endl;
}
I really don't see why it won't just print on the screen what I want it too. I even copied the exact code from a book of mine but it still does not do what I want it to. Why?
int main()
{
void WhyDoesThisNotWork();
return 0;
}
Just the way you can forward declare functions in global scope, you can also do the same in function scope. And what you simply did was to re-declare the function named WhyDoesThisNotWork that takes no argument and returns a void. You can declare a function multiple times. To call it, you simply do:
int main()
{
WhyDoesThisNotWork();
return 0;
}
Both lines void WhyDoesThisNotWork(); are (correct) function declarations (also known as "prototypes"). You're allowed to have more than one forward declaration for the same function, and you do.
What you don't have is a call to the function. A function call uses the function name (or a pointer to the function) and the values of any arguments. The call does not name the types of the arguments or the type of the function return itself.
double pow(double base, double exponent);
// ^^ has ^^ ^^ types, is a declaration
auto pow(double base, double exponent) -> double;
// ^^ ^^ ^^ ^^
// same declaration using funny new "trailing return type" syntax, also has types.
pow(4.0, 4.5);
// ^^ no types, is a function call
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am new to C++ and I need to create a function with this structure:
BOOL myfunc(LPDWORD myLpdword){ // myLpdword must be an out parameter
DWORD myDword = 1;
myLpdword = &myDword;
return true;
}
int main(){
DWORD outDword = 20;
myfunc(&outDword);
cout << outDword << end1;
return 0;
}
I expected that the value of outDword would be 1 (changed by myfunc), but the value is not changed by myfunc.
Please, can you give me a hint to solve this problem?
Like this
BOOL myfunc(LPDWORD myLpdword){ // myLpdword must be an out parameter
*myLpdword = 1;
return true;
}
Out parameter is not something that means anything in C++. MS use it but only because they are using a consistent terminology across different languages.
In C++ terms what you did is pass a pointer to the variable you want to modify to myfunc. What the above code does is take that pointer and dereference with the * operator it to modify the variable you wanted modified.
I like that you're writing small test programs to check your understanding of C++. But as others said there's no real substitute for a decent book. Any C++ book is going to cover this.
You passed in a pointer to outDword.
myLpdword is now a pointer to outDword.
You then changed myLpdword to be a pointer to myDword.
You never did anything with the VALUE of outDword.
You assigned the pointer of a variable that will not exist after exiting the function body (read on scopes in C/C++.
To solve your problem, assign the value of the variable to the dereferenced pointer, like so: *myLpdword = myDword;. It would also be wise to check that the value of the pointer is not null before dereferencing it like this: if (myLpdword == 0) { return; } . This check doesn't guarantee that the pointer is safe to assign to, but Atleast guards you against null pointer access.
In C++ this is called pass-by-reference. You denote it with an ampersand in the function signature:
bool myfunc(DWORD &myDword) {…
The ampersands you are using now are actually getting the address of the variables, so you should remove those.