How to pass time_t by reference into a function - c++11

I have a function definition like this:
double getPriceTimeByPtr(const StrategyParams* ptr, const int applied_price, const int timeframe, const int shift, const int shift_delta, const bool normalized, time_t &time);
but when I compile this code it errors just before the &time part so clearly there is a problem passing a time_t object by reference.
How do I fix this please?
I recently added the time_t parameter to the function and the error occurred since then.
Here's the errors generated:
Severity Code Description Project File Line Suppression
State
Error C2143 syntax error: missing ')' before '&'
Error C2143 syntax error: missing '{' before '&'
Error C2059 syntax error: '&'
Error C2059 syntax error: ')'
The syntax seems correct to me but the compiler doesn't like it.

The project that was failing was defined as a C project which was then trying to call a C++ function by reference. C can't us by reference so was failing. Changing the time parameters to pointers worked.

Related

Why an argument can be outside of ( ) in a function definition?

I have a piece of code in my .y file as follows
void yyerror(s) char *s; {
fputs(s, stderr);
putc('\n', stderr);
}
The function prototype declaration is in another file as follows
void yyerror (char const *s);
I tried to compile the code on both Linux and mac. On Linux, the compiler generates the following error message
error: argument ‘s’ doesn’t match prototype
void yyerror(s) char *s; {
^
On mac, the compiler generates the following warning message
warning: promoted type 'char *' of K&R function parameter is not compatible with the parameter type 'const char *' declared in a previous prototype [-Wknr-promoted-parameter]
void yyerror(s) char *s; {
^
My questions are:
Why clang only generates warning while cc generates an error.
Both compilers complain about 's' in "char *s". Why they ignore the 's' in () but consider the second 's' as the argument?

C++ iterator mismatch error

I am keeping track if instances of my class using std::vector to store pointers to all of the class objects. I'm wrapping things up and want to remove the pointer in the destructor... but I am getting the following error:
Brazos.cpp:15:89: error: cannot convert 'std::vector::iterator {aka __gnu_cxx::__normal_iterator >}' to 'const char*' for argument '1' to 'int remove(const char*)'
instanceAddress.erase(std::remove(instanceAddress.begin(), instanceAddress.end(), this) instanceAddress.end());
it seems I may need to dereference the iterator... Here is my code:
std::vector<Brazos*> Brazos::instanceAddress;
Brazos::Brazos(Mano mano)
{
instanceAddress.push_back(this);
_mano = mano;
}
Brazos::~Brazos(void)
{
instanceAddress.erase(std::remove(instanceAddress.begin(), instanceAddress.end(), this) instanceAddress.end());
}
You're missing a comma:
instanceAddress.erase(std::remove(instanceAddress.begin(), instanceAddress.end(), this), instanceAddress.end());
^
Also, the error message refers to int std::remove(const char*), so make sure you have #include <algorithm> for the correct std::remove.

Anonymous structs issues in union

I have something like this:
union MyBBox3D
{
struct
{
float m_fBox[6];
float m_fCenter[3];
float m_fDiagonalLen;
float m_fNormalizeFactor;
float m_fScaling[3];
};
struct
{
float m_fMin[3];
float m_fMax[3];
float m_fCenter[3];
float m_fDiagonalLen;
float m_fNormalizeFactor;
float m_fScaling[3];
};
struct
{
float m_fMinX, m_fMinY, m_fMinZ;
float m_fMaxX, m_fMaxY, m_fMaxZ;
float m_fCenterX, m_fCenterY, m_fCenterZ;
float m_fDiagonalLen;
float m_fNormalizeFactor;
float m_fScalingX, m_fScalingY, m_fScalingZ;
};
};
It compiles well with vs2008 and intel compiler 12.0, but cant be compiled with gcc4.6.3, it gives the following errors:
In file included from Mesh/MyMeshTool.cpp:17:0:
Mesh/MyMeshTool.h:68:28: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fCenter [3]’
Mesh/MyMeshTool.h:59:28: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fCenter [3]’
Mesh/MyMeshTool.h:69:17: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:60:17: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:70:20: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
Mesh/MyMeshTool.h:61:20: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
Mesh/MyMeshTool.h:71:32: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fScaling [3]’
Mesh/MyMeshTool.h:62:32: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fScaling [3]’
Mesh/MyMeshTool.h:78:17: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:60:17: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:79:20: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
Mesh/MyMeshTool.h:61:20: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
How can I solve this problem? Thanks in advance!
I think in this case (where the separate structures making up the union share identifier names), you're probably better off not using anonymous structures.
Either that or make the names unique across the entire union.
Anything else is just asking for trouble :-)
Recent GCC do accept unamed fields if you pass the -fms-extensions program option to gcc invocation, but this is an extension do the standard C99 specification.
You could also use preprocessor tricks with care, e.g.
union myunion {
struct {
int fa_u1;
int fb_u1;
} u1;
#define fa u1.fa_u1
#define fb u1.fb_u1
struct {
double fc_u2;
char fd_u2[8];
} u2;
#define fc u2.fc_u2
#define fd u2.fd_u2
}
Then you can code x.fa instead of x.u1.fa_u1 etc. Use preprocessor tricks with care and caution (remember that a #define has full translation unit scope). In practice you'll want the fa, u1, fa_u1 names to be long and unique.

cvCreateButton VS2010

When I use the code
cvCreateButton("button1",callbackButton2,NULL,CV_PUSH_BUTTON,1);
void callbackButton2(int state, void *pointer){
int i = 2;
return;
}
I get the following error in visual studios 2010
error C2065: 'callbackButton2' : undeclared identifier
Could someone please explain what I am doing wrong to cause this error
Thanks
Based on the code presented it appears that callbackButton2 is used on the first line but is declared after this on the second line.
In most languages this is not a legal thing to do because an identifier is not valid until the compiler can determine the context(s) in which it can be used which is what declaring an identifier does
Try this:
void callbackButton2(int state, void *pointer){
int i = 2;
return;
}
cvCreateButton("button1",callbackButton2,NULL,CV_PUSH_BUTTON,1);
In that code sample the identifier callbackButton2is declared as a function before being used as an argument to the cvCreateButton function and should therefore be a valid use of the identifier

V8 compilation problems

I'm trying to compile a file with the V8 the JavaScript Engine by Google. I installed scons and have compiled the V8 engine. But, here is where the problem lies, I stay in the V8 directory as they say and make a file named hello_world.cpp with the code:
#include <v8.h>
using namespace v8;
int main(int argc, char* argv[]) {
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Create a new context.
Persistent<Context> context = Context::New();
// Enter the created context for compiling and
// running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Handle<String> source = String::New("'Hello' + ', World!'");
// Compile the source code.
Handle<Script> script = Script::Compile(source);
// Run the script to get the result.
Handle<Value> result = script->Run();
// Dispose the persistent context.
context.Dispose();
// Convert the result to an ASCII string and print it.
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
return 0;
}
Then I compile using gcc hello_world.cpp -o libv8.a. But, when I compile it I get a skew of errors:
hello_world.cpp:1:16: error: v8.h: No such file or directory
hello_world.cpp:3: error: ‘v8’ is not a namespace-name
hello_world.cpp:3: error: expected namespace-name before ‘;’ token
hello_world.cpp: In function ‘int main(int, char**)’:
hello_world.cpp:8: error: ‘HandleScope’ was not declared in this scope
hello_world.cpp:8: error: expected `;' before ‘handle_scope’
hello_world.cpp:11: error: ‘Persistent’ was not declared in this scope
hello_world.cpp:11: error: ‘Context’ was not declared in this scope
hello_world.cpp:11: error: ‘context’ was not declared in this scope
hello_world.cpp:11: error: ‘Context’ is not a class or namespace
hello_world.cpp:15: error: ‘Context’ is not a class or namespace
hello_world.cpp:15: error: expected `;' before ‘context_scope’
hello_world.cpp:18: error: ‘Handle’ was not declared in this scope
hello_world.cpp:18: error: ‘String’ was not declared in this scope
hello_world.cpp:18: error: ‘source’ was not declared in this scope
hello_world.cpp:18: error: ‘String’ is not a class or namespace
hello_world.cpp:21: error: ‘Script’ was not declared in this scope
hello_world.cpp:21: error: ‘script’ was not declared in this scope
hello_world.cpp:21: error: ‘Script’ is not a class or namespace
hello_world.cpp:24: error: ‘Value’ was not declared in this scope
hello_world.cpp:24: error: ‘result’ was not declared in this scope
hello_world.cpp:30: error: ‘String’ is not a class or namespace
hello_world.cpp:30: error: expected `;' before ‘ascii’
hello_world.cpp:31: error: ‘ascii’ was not declared in this scope
hello_world.cpp:31: error: ‘printf’ was not declared in this scope
I don't get why it say V8.h is not declared. I already built it and I'm in its directory and I'm guessing if I get rid of that all the other errors will go away. Any suggestions?
i just believe you in that you are really inside the toplevel source directory (and since i do not have compiled v8 i only believe that libvp8.a is created in that toplevel directory):
% g++ -Iinclude hello_world.cpp -o hello_world libv8.a
it says "v8.h" is not declared because that file is inside the "include" directory and the preprocessor is not able to find it out of thin air.
furthermore: you are compiling a .cpp file with the C compiler instead of the C++ compiler.
you are using the '-o' flag wrong because it defines the name of the linked binary and thus needs a name, you do not want the output binary be named "libvp8.a"

Resources