Simple C++ exception error (MEX) - c++

I am working on adapting some C++ code from Windows to Linux to compile as mex in Matlab. The program is designed to connect a data glove via a virtual serial port. I've never worked much at all with C++ before, so the solution this issue could very well be quite simple. In a larger file, I am getting issues caused by the following try/catch:
try
{
// You can tell which com port to use by checking windows settings under hardware
glove = new CyberGlove(portName, (int) *baudRate); // baudrate 115200
/* Return CyberGlove object */
mxArray* result = (mxArray*) mxMalloc(sizeof(CyberGlove));
*reinterpret_cast<CyberGlove*>(result) = *glove;
plhs[0] = result;
}
catch(std::runtime_error err)
{
std::cout << "Error: " << err.what() << std::endl;
}
delete glove;
When I try to compile it in Matlab, I get the following errors:
InitCyberGlove.cpp: In function ‘void mexFunction(int, mxArray**, int, const mxArray**)’:
InitCyberGlove.cpp:43:11: error: expected type-specifier
InitCyberGlove.cpp:43:30: error: expected unqualified-id before ‘err’
InitCyberGlove.cpp:43:30: error: expected ‘)’ before ‘err’
InitCyberGlove.cpp:43:30: error: expected ‘{’ before ‘err’
InitCyberGlove.cpp:43:30: error: ‘err’ was not declared in this scope
InitCyberGlove.cpp:43:33: error: expected ‘;’ before ‘)’ token
I don't think it's specifically related to the CyberGlove stuff so much as my general lack of knowledge of C++ and how exceptions work.

You need to #include <stdexcept> where std::runtime_error is declared.

Related

How to write a program that displays numeric limits of a data type in a table [closed]

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 1 year ago.
Improve this question
I'm currently learning how to program in C++ and one of the practical examples is to write a program showing the data types numeric limit in a table. Currently writing in repl.it before pasting to .txt and compiling using makefile. There are no resources or similar examples I could find explaining how to do this, nor have I fully grasped enough to ID the correct keywords to use.
Question
Attempt:
#include <limits>
void main()
{
number = sizeor(int)
unsign = sizeor(unsigned int)
long = sizeor(long)
longlong = sizeor(long_long)
unsignedchar = sizeor(unsigned char)
float = sizeor(float)
double = sizeor(double)
char = sizeor(char)
cout<<numeric_limits<int>::min();
cout<<numeric_limits<int>::max();
cout<<numeric_limits<int>::epsilon();
cout<<numeric_limits<double>::min();
cout<<numeric_limits<double>::max();
cout<<numeric_limits<double>::epsilon();
cout<<numeric_limits<unsign>::min();
cout<<numeric_limits<unsign>::max();
cout<<numeric_limits<unsign>::epsilon();
cout<<numeric_limits<long>::min();
cout<<numeric_limits<long>::max();
cout<<numeric_limits<long>::epsilon();
cout<<numeric_limits<longlong>::min();
cout<<numeric_limits<longlong>::max();
cout<<numeric_limits<longlong>::epsilon();
cout<<numeric_limits<unsignedchar>::min();
cout<<numeric_limits<unsignedchar>::max();
cout<<numeric_limits<unsignedchar>::epsilon();
cout<<numeric_limits<float>::min();
cout<<numeric_limits<float>::max();
cout<<numeric_limits<float>::epsilon();
cout<<numeric_limits<char>::min();
cout<<numeric_limits<char>::max();
cout<<numeric_limits<char>::epsilon();
}
Except this prints
main.cpp:5:2: error: expected function body after function declarator
number = sizeor(int)
^
main.cpp:15:2: error: unknown type name 'cout'
cout<<numeric_limits<int>::max();
^
main.cpp:15:6: error: expected unqualified-id
cout<<numeric_limits<int>::max();
^
main.cpp:16:2: error: unknown type name 'cout'
cout<<numeric_limits<int>::epsilon();
^
main.cpp:16:6: error: expected unqualified-id
cout<<numeric_limits<int>::epsilon();
^
main.cpp:18:2: error: unknown type name 'cout'
cout<<numeric_limits<double>::min();
^
main.cpp:18:6: error: expected unqualified-id
cout<<numeric_limits<double>::min();
^
main.cpp:19:2: error: unknown type name 'cout'
cout<<numeric_limits<double>::max();
^
main.cpp:19:6: error: expected unqualified-id
cout<<numeric_limits<double>::max();
^
main.cpp:20:2: error: unknown type name 'cout'
cout<<numeric_limits<double>::epsilon();
^
main.cpp:20:6: error: expected unqualified-id
cout<<numeric_limits<double>::epsilon();
^
main.cpp:22:2: error: unknown type name 'cout'
cout<<numeric_limits<unsign>::min();
^
main.cpp:22:6: error: expected unqualified-id
cout<<numeric_limits<unsign>::min();
^
main.cpp:23:2: error: unknown type name 'cout'
cout<<numeric_limits<unsign>::max();
^
main.cpp:23:6: error: expected unqualified-id
cout<<numeric_limits<unsign>::max();
^
main.cpp:24:2: error: unknown type name 'cout'
cout<<numeric_limits<unsign>::epsilon();
^
main.cpp:24:6: error: expected unqualified-id
cout<<numeric_limits<unsign>::epsilon();
^
main.cpp:26:2: error: unknown type name 'cout'
cout<<numeric_limits<long>::min();
^
main.cpp:26:6: error: expected unqualified-id
cout<<numeric_limits<long>::min();
What is the correct method to acquire the desired result and where/what are the deficiencies in my attempt that I need to correct?
There're quite a lot of errors/typos within your code.
void main() will return a '::main' must return 'int' error, the correct syntax is int main().
number = (IMO is Pythonic syntax) should be int number = , as in C++, the correct format for declaring variables is type variable_name = value;. More info here.
Variables declarations should all end with ;, or the program will return a error: expected ',' or ';' before 'int'.
A keyword such as int or char mustn't be used as a variable name.
sizeor() is not a valid operator, it's sizeof() as specified clearly in your question.
cout and numeric_limits<int>::min() is invalid, it should be std::cout and std::numeric_limits<int>::min(), or you can use using namespace std; instead. Also, when using cout, #include <iostream> must be used.
Some of the type you used is invalid, such as long_long should be long long, unsign should be unsigned int and unsignedchar should be unsigned char.
I include a little snippet of your code which have been modified here (you can build and fill in the rest from this):
#include <limits>
#include <iostream>
int main()
{
int number = sizeof(int);
int unsign = sizeof(unsigned int);
int long_ = sizeof(long);
int longlong = sizeof(long long);
int unsignedchar = sizeof(unsigned char);
int float_ = sizeof(float);
int double_ = sizeof(double);
int char_ = sizeof(char);
std::cout<<std::numeric_limits<int>::min() << "\n";
std::cout<<std::numeric_limits<unsigned char>::min() << "\n";
std::cout<<std::numeric_limits<long long>::min() << "\n";
std::cout<<std::numeric_limits<float>::min() << "\n";
}
Result:
-2147483648
-9223372036854775808
1.17549e-38
It should also be noticed that sizeof() actually "Yields the size in bytes of the object representation of type", not bits. As #Nathan Pierson mentioned, the number of bits is 8 times more than the number of bytes.
P.S: As #tadman mentioned above:
Instead of typing all of this code in and then mashing the compile
button, start with the most minimal program, ensure it compiles, and
build incrementally from there. Compile frequently. Stop adding more
code the instant you have a compile problem, fix that issue before
making things worse.
And #JaMiT :
Building on what tadman wrote, your first error is on the first line
in your main function. Try reducing your program to #include <limits> void main() { number = sizeor(int) } and focusing on what is
preventing that much from compiling. (A minimal reproducible example
is a powerful debugging tool; it doesn't matter that there is no
useful functionality yet.)
IMO, you're not really familiar with C++ syntaxes, maybe you should try some simpler program first to get the hang of it. The list of books #Richard Critten provided is very useful for beginners.
More info:
std::numeric_limits : https://en.cppreference.com/w/cpp/types/numeric_limits
sizeof() : https://en.cppreference.com/w/cpp/language/sizeof
std::cout : https://en.cppreference.com/w/cpp/io/cout
sizeof is an operator and keyword in c++, and it calculates the bytes of data type or variables during the complying time. remember that sizeof is an operator, not a function.

Getting an weird error in vscode when i'm trying to run a code

When I'm trying to run a simple hello world script it sends this error message, Can anyone help me solving this issue?
[Running] cd "c:\Users\NickT\OneDrive\Documents\C++ Tutorial\" && g++ helloworld.cpp -o helloworld && "c:\Users\NickT\OneDrive\Documents\C++ Tutorial\"helloworld
helloworld.cpp: In function `int main()':
helloworld.cpp:9: error: expected primary-expression before "msg"
helloworld.cpp:9: error: expected `;' before "msg"
helloworld.cpp:11: error: expected primary-expression before "const"
helloworld.cpp:11: error: expected `;' before "const"
helloworld.cpp:16: error: expected primary-expression before '}' token
helloworld.cpp:16: error: expected `)' before '}' token
helloworld.cpp:16: error: expected primary-expression before '}' token
helloworld.cpp:16: error: expected `;' before '}' token
[Done] exited with code=1 in 0.233 seconds
This is the written code
#include <iostream>
int main(){
std::cout << "Hello World" << std::endl;
return 0;
}
I revised the code you have posted as well as added a piece of code to utilize the namespace std. Analyzing the output from your compiler it does seem that you may have not added a semi colon. Alternatively, it's not necessary to reference the std library when calling it's functions but it's up to your preference. It doesn't seem though to be an issue with your code either. Can you post the actual code you've attempted to compile because there doesn't seem to be an issue with this piece.
Revised Code:
#include <iostream>
using namespace std;
int main() {
std::cout << "Hello World" << std::endl;
return 0;
}

Using unordered_map on my g++ (5.1.0) compiler in command prompt shows error

I have recently downloaded MinGW into my computer but on using certain containers and iterators like unordered_map and auto it shows an unexpected error.
my code is as follows :
#include <bits/stdc++.h>
#include<unordered_map>
using namespace std;
int main()
{
unordered_map<string, int> umap;
umap["GeeksforGeeks"] = 10;
umap["Practice"] = 20;
umap["Contribute"] = 30;
for (auto x : umap)
cout << x.first << " " << x.second << endl;
return 0;
}
it gives the following error :
C:\Users\naima\Documents\cpp>g++ -o try2 try2.cpp
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/unordered_map:35:0,
from try2.cpp:2:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
try2.cpp: In function 'int main()':
try2.cpp:9:5: error: 'unordered_map' was not declared in this scope
unordered_map<string, int> umap;
^
try2.cpp:9:25: error: expected primary-expression before ',' token
unordered_map<string, int> umap;
^
try2.cpp:9:27: error: expected primary-expression before 'int'
unordered_map<string, int> umap;
^
try2.cpp:11:5: error: 'umap' was not declared in this scope
umap["GeeksforGeeks"] = 10;
^
try2.cpp:15:15: error: 'x' does not name a type
for (auto x : umap)
^
try2.cpp:19:5: error: expected ';' before 'return'
return 0;
^
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ';' before 'return'
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ')' before 'return'
The compiler told you exactly what was wrong. It usually will.
This file requires compiler and library support for the ISO C++ 2011 standard. This
support is currently experimental, and must be enabled with the -std=c++11 or
-std=gnu++11 compiler options.
You just have to compile with the proper flag, -std=c++11. I don't know if you are version-matching against what graders use or what, but there are very few good reasons to be on a minGW compiler where support for an 8 year old standard is still considered experimental.
You can see that it works as expected here: https://godbolt.org/z/JQxL00
If you remove the -std=c++11 flag, it will fail to compile and give you the same error message.
You might also notice that I altered the includes to only include what I use. This results in a much faster compile time, smaller executable, and an easier to understand piece of code (Since it is plain to see what standard features are being used). You also avoid polluting your namespace.

Which functions get called when you send 0 as argument [closed]

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
void func_print(int value) {
std::cout << “int” << std::endl;
}
void func_print(void* value) {
std::cout << “void” << std::endl;
}
int main() {
func_print(0);
func_print(NULL);
}
I cant seem to find. My compiler may be broken because it gives me stray errors cant find it what is wrong.
g++: error: -E or -x required when input is from standard input
c.cpp:3:5: error: stray ‘\342’ in program
std::cout << “int” << std::endl;
^
c.cpp:3:5: error: stray ‘\200’ in program
c.cpp:3:5: error: stray ‘\234’ in program
c.cpp:3:5: error: stray ‘\342’ in program
c.cpp:3:5: error: stray ‘\200’ in program
c.cpp:3:5: error: stray ‘\235’ in program
c.cpp:6:5: error: stray ‘\342’ in program
std::cout << “void” << std::endl;
^
c.cpp:6:5: error: stray ‘\200’ in program
c.cpp:6:5: error: stray ‘\234’ in program
c.cpp:6:5: error: stray ‘\342’ in program
c.cpp:6:5: error: stray ‘\200’ in program
c.cpp:6:5: error: stray ‘\235’ in program
c.cpp: In function ‘void func_print(int)’:
c.cpp:3:21: error: expected primary-expression before ‘int’
std::cout << “int” << std::endl;
^
c.cpp: In function ‘void func_print(void*)’:
c.cpp:6:21: error: expected primary-expression before ‘void’
std::cout << “void” << std::endl;
^
c.cpp: In function ‘int main()’:
c.cpp:11:20: error: call of overloaded ‘func_print(NULL)’ is ambiguous
func_print(NULL);
^
c.cpp:2:6: note: candidate: void func_print(int)
void func_print(int value) {
^
c.cpp:5:6: note: candidate: void func_print(void*)
void func_print(void* value) {
All errors here explained with messages though i don't get it exactly what is wrong.
Which functions get called when you send 0 as argument
The call is ambiguous. Neither function is preferred by the overload resolution, since 0 is both an int literal, as well as a pointer literal. An ambiguous call makes the program ill-formed, so a compiler is not required to accept it. This is what the compiler told you:
error: call of overloaded ‘func_print(NULL)’ is ambiguous
std::cout << “void” << std::endl;
This is wrong, because “ (Left Double Quotation Mark) is not a valid character there. You've most likely attempted to write a string literal. A string literal uses the " (Quotation Mark) character, which is similar. This is what the compiler told you:
error: expected primary-expression before ‘void’
It doesnt have any include or headers it should compile by its own (supposedly)
Your supposition is wrong. Besides the problems mentioned earlier, std::cout (or anything else from the std namespace) cannot be used without including standard headers.

MYSQL headers conflict with STL <algorithm> in C++

// File test.cpp
#include <my_global.h>
#include <algorithm>
int main()
{
return 0;
}
Compiled with: g++ -c -I /usr/local/mysql/include/mysql/ test.cpp, where /usr/local/mysql is the mysql install directory.Then the compiler report the following errors:
In file included from /usr/include/c++/4.4/algorithm:61,
from test.cpp:3:
/usr/include/c++/4.4/bits/stl_algobase.h:232:56: error: macro "min" passed 3 arguments, but takes just 2
/usr/include/c++/4.4/bits/stl_algobase.h:253:56: error: macro "max" passed 3 arguments, but takes just 2
In file included from /usr/include/c++/4.4/bits/stl_algo.h:61,
from /usr/include/c++/4.4/algorithm:62,
from test.cpp:3:
/usr/include/c++/4.4/bits/algorithmfwd.h:353:41: error: macro "max" passed 3 arguments, but takes just 2
/usr/include/c++/4.4/bits/algorithmfwd.h:364:41: error: macro "min" passed 3 arguments, but takes just 2
In file included from /usr/include/c++/4.4/algorithm:61,
from test.cpp:3:
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected unqualified-id before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected initializer before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected unqualified-id before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected initializer before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:232: error: ‘std::min’ declared as an ‘inline’ variable
/usr/include/c++/4.4/bits/stl_algobase.h:232: error: template declaration of ‘const _Tp& std::min’
/usr/include/c++/4.4/bits/stl_algobase.h:235: error: expected primary-expression before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:235: error: expected ‘}’ before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:237: error: expected unqualified-id before ‘return’
/usr/include/c++/4.4/bits/stl_algobase.h:253: error: ‘max’ declared as an ‘inline’ variable
/usr/include/c++/4.4/bits/stl_algobase.h:253: error: template declaration of ‘const _Tp& max’
/usr/include/c++/4.4/bits/stl_algobase.h:256: error: expected primary-expression before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:256: error: expected ‘}’ before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:258: error: expected unqualified-id before ‘return’
/usr/include/c++/4.4/bits/stl_algobase.h:259: error: expected declaration before ‘}’ token
I think that there's some name conflict between my_global.h and algorithm, so I wrap my_global.h in a namespace:
// File test.cpp
namespace MYSQL_NAMESPACE {
#include <my_global.h>
}
#include <algorithm>
int main()
{
return 0;
}
But it doesn't help, the compiler still report the same errors. Then I change the include order as following:
// File test.cpp
#include <algorithm>
#include <my_global.h>
int main()
{
return 0;
}
Every thing goes well now.
Does Anybody Really Know What The Problem It Is?
TKS!
It seems that the mysql header defines a macro min.
#if !defined(max)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
This has been reported to MySQL as bug 28184. The bug is marked as closed, so try updating to the newest version. According to the bug page it should be fixed in version 5.1.23, version 6.0.4 and newer versions.
Apparently the namespace trick does not work because min/max are macros and the preprocessor does not look at namespace scope.
This might fix the problem:
#include <my_global.h>
#undef min
#undef max
#include <algorithm>
The whole thing looks horrible though :)
It looks like my_global.h defines some name used by algorithm as a preprocessor macro, causing compilation to fail. With the ordering that works, you won't be able to use whatever it is that my_global.h clobbers, but your code will at least compile unless you need that feature. Since preprocessor macros are not namespaced, the namespace wrapping will not help, as you have observed.
Therefore, it sounds like my_global.h is broken, but if everything works just use the include order that works and go with it. That's my preferred include order anyway - standard library headers first, followed by external library headers, followed by internal headers.