This question already has an answer here:
c++ - Doesn't name a type
(1 answer)
Closed 6 months ago.
I edit the code to clarify the actual code :
#include <fstream>
#include <iostream>
#include <ros/ros.h>
#include <rosbag/bag.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>
#include <nav_msgs/Odometry.h>
std::ofstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
void callhandler(const nav_msgs::Odometry::ConstPtr& msg)
{
runtimeFile.open();
if (!runtimeFile)
{
std::cout << "cmg_operations_runtime.txt could not be opened.";
}
runtimeFile << "tempVector[j]" << ";\t";
runtimeFile.close ();
std::cout << "Runtime data stored." << std::endl;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "main");
ros::NodeHandle nh;
ros::Subscriber Listener = nh.subscribe<nav_msgs::Odometry>("/odom", 100, callhandler);
ros::spin();
return 0;
}
error: `‘runtimeFile’ does not name a type
9 | runtimeFile.open ("cmg_operations_runtime.txt")
The error is the same, I hope someone to help me in this issue?`
In C++ all code must be inside a function. Additionally all C++ programs must have a function called main.
Further your code opens the file twice, once when you declare the runtimeFile variable and once when you call open. Did you not think it strange that you have the file name twice in your code? Don't open files twice. Finally, although it's not an error, there is no need to close the file, that will happen automatically.
Put all that together and you have a legal C++ program.
#include <fstream>
int main()
{
std::fstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
runtimeFile << "tempVector[j]" << ";\t";
}
EDIT
Some real code has been posted. Based on that I would remove the global runtimeFile variable and make it local to callHandler like the following
void callhandler(const nav_msgs::Odometry::ConstPtr& msg)
{
std::ofstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
if (!runtimeFile)
{
std::cout << "cmg_operations_runtime.txt could not be opened.";
}
runtimeFile << "tempVector[j]" << ";\t";
std::cout << "Runtime data stored." << std::endl;
}
However I can't really see how the latest posted code causes the error described.
Trying to write a cpp code to print out messages from camera image using darknet. I built a class in which there is mutex method which I use for utilizing callback message in multiple threads. Although catkin_make builds the file successfully, it gives segmentation error when I run the ros command with rosrun . The code is as follows:
#include "ros/ros.h"
#include "darknet_ros_msgs/BoundingBoxes.h"
#include "darknet_ros_msgs/BoundingBox.h"
#include<string>
#include<thread>
#include<iostream>
#include <mutex>
#include "geometry_msgs/Twist.h"
class Firstolo
{
private:
std::mutex yolo_mtx;
darknet_ros_msgs::BoundingBoxes last_yolo_msg;
public:
void callback(const darknet_ros_msgs::BoundingBoxes& msg)
{
std::lock_guard<std::mutex> lck(yolo_mtx);
last_yolo_msg = msg;
}
const darknet_ros_msgs::BoundingBoxes getYoloLastMsg()
{
std::lock_guard<std::mutex> lck(yolo_mtx);
return last_yolo_msg;
}
void dothejob()
{
std:: cout << "Here it goes: " << getYoloLastMsg().bounding_boxes[0].xmin << std::endl;
std:: cout << "Here it goes: " << getYoloLastMsg().bounding_boxes[0].xmax << std::endl;
std:: cout << "\033[2J\033[1;1H";
}
Firstolo()
{
}
Firstolo(Firstolo&)
{
std::mutex yolo_mtx;
}
~Firstolo()
{
}
};
int main( int argc, char **argv)
{
ros::init(argc,argv,"cood_subscriber");
Firstolo nc;
ros::NodeHandle nh;
ros::Subscriber sub;
sub = nh.subscribe("/darknet_ros/bounding_boxes", 100, &Firstolo::callback, &nc);
nc.dothejob();
ros::spin();
return 0;
}
Edit: It turns out that the problem is in the void dothejob(). I added std::lock_guardstd::mutex lck(yolo_mtx); to the void dothejob() and Segmentation error no longer shows up. Now the only remaining problem is that std:: cout << "Here it goes: " << getYoloLastMsg().bounding_boxes[0].xmin << std::endl; line keeps waiting for messages rather than printing them out. In fact, messages naturally should appear since there is darknet running in the background and generating messages.
While I'm trying to learn throw catch I just compiled my code and I found this output what does that mean?
#include "stdafx.h"
#include <iostream>
using namespace std;
void MightGoWrong() {
bool error = true;
if (error) {
throw 8;
}
// -------------------------
int main()
{
cout << MightGoWrong;
return 0;
}
And output is : 012211A4 what does that mean?
Output
Code
You are not calling your function.
cout << MightGoWrong; is simply printing the address of the function. To call it you should do cout << MightGoWrong();.
This question already has answers here:
How to catch segmentation fault in Linux?
(5 answers)
Catching access violation exceptions?
(8 answers)
Closed 6 years ago.
I need to catch segmentation fault and other unknown exceptions in my application. But I do not know how I can do that!
Can I use std::uncaught_exceptions for this aim?
Can I use std::uncaught_exceptions for this aim?
Consider this code:
int main(int argc, char* argv[])
{
int *val = NULL;
*val = 1;
std::cout << "uncaught: " << std::uncaught_exceptions() << std::endl;
return 0;
}
This will likely cause a segmentation fault and nothing will be output.
I need to catch segmentation fault and other unknown exceptions in my application. But I do not know how I can do that!
Exception handling in C++ can be done through the try-catch block, and you could use the std::signal function to catch certain errors like SIGSEGV, SIGFPE, or SIGILL, example:
#include <iostream>
#include <exception>
#include <csignal>
#include <cstdio>
extern "C" {
void sig_fn(int sig)
{
printf("signal: %d\n", sig);
std::exit(-1);
}
}
int main(int argc, char* argv[])
{
int *val = NULL;
std::signal(SIGSEGV, sig_fn);
try {
*val = 1;
} catch (...) {
std::cout << "..." << std::endl;
}
if (std::uncaught_exception()) {
std::cout << "uncaught" << std::endl;
}
std::cout << "return" << std::endl;
return 0;
}
But you should note that this type of exception handling is really meant to do clean-up and shutdown, not necessarily catch and release; take this code for example:
#include <iostream>
#include <exception>
#include <csignal>
#include <cstdio>
extern "C" {
void sig_fn(int sig)
{
printf("signal: %d\n", sig);
}
}
int main(int argc, char* argv[])
{
int *val = NULL;
std::signal(SIGSEGV, sig_fn);
while (true) {
try {
*val = 1;
} catch (...) {
std::cout << "..." << std::endl;
}
}
if (std::uncaught_exception()) {
std::cout << "uncaught" << std::endl;
}
std::cout << "return" << std::endl;
return 0;
}
This code will cause and catch the segmentation fault forever!
If you are trying to catch a segmentation fault, you need to investigate why the segmentation fault (or any error for that matter) happened in the first place and correct that issue; using the above code as an example:
int *val = NULL;
if (val == NULL) {
std::cout << "Handle the null!" << std::endl;
} else {
*val = 1;
}
For further reading: here is a SO Q&A on what a segfault is, as well, here is the Wiki on it, and MIT has some tips on handling and debugging segfaults too.
Hope that can help.
Properly testing function return values is fundamental, but it can quickly clutter the code and make it hard to read, like in the simple example below:
#include <iostream>
#include <fstream>
int main(int argc, char **argv)
{
std::string filename("/usr/include/malloc.h");
std::ifstream ifs(filename.c_str());
if (!ifs.is_open())
{
std::cerr << "Failed to open file " << filename << std::endl;
return 1;
}
ifs.close();
std::cout << "Passed the first error handling" << std::endl;
filename = "/this/file/does/not/exist";
ifs.open(filename.c_str());
if (!ifs.is_open())
{
std::cerr << "Failed to open file " << filename << std::endl;
return 1;
}
return 0;
}
I have thought of a solution reducing cluttering by using a macro and c++11 lambda functions like this:
#include <iostream>
#include <fstream>
#define RETURN_IF(X,Y,Z) if ( X ) { Y ; return Z; }
auto open_file_error = [](const std::string& filename)
{
std::cerr << "Failed to open file " << filename << std::endl;
};
int main(int argc, char **argv)
{
std::string filename("/usr/include/malloc.h");
std::ifstream ifs(filename.c_str());
RETURN_IF (!ifs.is_open(), open_file_error(filename), 1 );
ifs.close();
std::cout << "Passed the first error handling" << std::endl;
filename = "/this/file/does/not/exist";
ifs.open(filename.c_str());
RETURN_IF (!ifs.is_open(), open_file_error(filename), 1 );
return 0;
}
As you can see, the main function is less cluttered. Do you think that there are drawbacks to doing it like that or could it be a method to largely use?
Note that I use several macros to handle cases with or without a return value, for testing equality with a value, etc.
I propose the new version below to take into account two things:
- the answers and comments about the preference on using exceptions instead of return values;
- put away the emphasis on std::ifstream specific errors which are not the subject of the question.
#include <iostream>
#include <fstream>
#include <exception>
class OurExceptionForTheExternalLibraryFailure : public std::exception {};
#define CLEANUP_AND_THROW_IF(X,Y,Z) if ( X ) { Y ; throw Z; }
/* Return true in case of succes and false otherwise */
bool anyExternalFunction(const std::string& aString)
{
std::ifstream ifs(aString.c_str());
if (ifs.is_open())
{
ifs.close();
return true;
}
else
{
return false;
}
}
auto this_external_function_error_cleanup = [](const std::string& aString)
{
std::cerr << "The external function failed " << aString << std::endl;
// other stuff
};
int main(int argc, char **argv)
{
try
{
std::string aString = "/usr/include/malloc.h";
bool functionResult = anyExternalFunction(aString);
CLEANUP_AND_THROW_IF (!functionResult, this_external_function_error_cleanup(aString), OurExceptionForTheExternalLibraryFailure() );
std::cout << "Passed the first error handling" << std::endl;
aString = "/this/file/does/not/exist";
functionResult = anyExternalFunction(aString);
CLEANUP_AND_THROW_IF (!functionResult, this_external_function_error_cleanup(aString), OurExceptionForTheExternalLibraryFailure() );
}
catch (const OurExceptionForTheExternalLibraryFailure& e)
{
std::cerr << "Catched OurExceptionForTheExternalLibraryFailure. There was an error" << std::endl;
}
return 0;
}
What do you think about this new version (which still uses a macro, though...) ?
Well, if you are already using lambdas, and you don't want all that testing code everywhere, you could always do something like (NOTE: uncompiled/untested code,)
template <typename FileReader>
void with_file(std::string file, FileReader&& reader) {
std::ifstream in(file);
if (in) {
reader(in);
} else {
throw std::runtime_error("Failed to open file: " + file); // NOTE: I'm being lazy here
}
}
int main(...) {
with_file("foo.txt", [](auto& in) {
// do something with the stream
});
}
.. but it's a matter of preference, I like exceptions, lambdas and small utility functions, but some may not...
This is pretty much a textbook example of when to use exceptions.
You don't, however, have to write your own code to test for a file opening correctly, and throwing an exception (and so on) when it fails. Iostreams already support that fairly directly, so you can write code something like this:
#include <fstream>
#include <iostream>
int main() {
try {
std::ifstream in("/usr/include/malloc.h");
in.exceptions(std::ios::failbit);
in.close();
std::cout << "passed first test.\n";
std::ifstream in2("/this/file/does/not/exist");
in2.exceptions(std::ios::failbit);
in2.close();
std::cout << "Passed second test\n";
}
catch (std::system_error &f) {
std::cerr << "Failed to open file: " << f.what() << "\n";
}
}
Of course, if you want to get the try/catch out of main, you can do that as well. I'm not sure you gain much from doing so though.
More generally, however, exceptions are clearly the right tool for this job. For other functions that don't provide a way to get exceptions reported as exceptions, you may have to write a wrapper of your own. Either way, however, if you have a function that has some range of normal return values, and one (or a few) "special" values in indicate failure (and similar) that's a pretty decent indication that it's indicating an exceptional condition via the return value--and the right way to deal with exceptional conditions is via exceptions rather than return values.
Rather than try to reiterate the (long) list of why/when/how to use exception handling, I'll refer you (as a starting point) to Herb Sutter's old article on when and how to use exceptions.
Suggest this as a much cleaner example. NOW USING EXCEPTIONS...
I haven't tested that it's 100% the same behaviour as your example (which I appreciate is just that; an example).
By the way, MFC has a "SUCCESS" macro that does a similar check to your "RETURN_IF". I don't like that macro either...
#include <iostream>
#include <fstream>
#include <string>
void TestForFileOpen(const std::string& filename)
{
std::ifstream ifs(filename.c_str());
if (!ifs.is_open())
{
throw std::exception("Failed");
}
}
void ReportFileOpenFailure(const std::string& filename)
{
std::cerr << "Failed to open file " << filename << std::endl;
}
void NoisyTestForFileOpen(const std::string& filename)
{
try
{
TestForFileOpen(filename);
}
catch(...)
{
ReportFileOpenFailure(filename);
throw;
}
}
int main(int argc, char **argv)
{
std::string filename("/usr/include/malloc.h");
try
{
NoisyTestForFileOpen(filename);
std::cout << "Passed the first error handling" << std::endl;
filename = "/this/file/does/not/exist";
NoisyTestForFileOpen(filename);
}
catch (...)
{
return 1;
}
return 0;
}
A more general example with a custom API:
#include <iostream>
#include <fstream>
#include <string>
class IFileTester
{
public:
virtual ~IFileTester() {}
// throws if file cannot be opened
virtual void TestForFileOpen(const std::string& filename) const = 0;
};
class IfStreamFileTester : public IFileTester // implement as many versions as you need
{
public:
virtual void TestForFileOpen(const std::string& filename) const
{
// implement this in terms of ifstream
std::ifstream ifs(filename.c_str());
// thanks #Jerry-Coffin
ifs.exceptions(std::ios::failbit);
}
};
void ReportFileOpenFailure(const std::string& filename)
{
std::cerr << "Failed to open file " << filename << std::endl;
}
void NoisyTestForFileOpen(const IFileTester& fileTester, const std::string& filename)
{
try
{
fileTester.TestForFileOpen(filename);
}
catch(...)
{
ReportFileOpenFailure(filename);
throw;
}
}
int main(int argc, char **argv)
{
IFileTester& fileTester = IfStreamFileTester();
std::string filename("/usr/include/malloc.h");
try
{
NoisyTestForFileOpen(fileTester, filename);
std::cout << "Passed the first error handling" << std::endl;
filename = "/this/file/does/not/exist";
NoisyTestForFileOpen(fileTester, filename);
}
catch (...)
{
return 1;
}
return 0;
}
Looks okay to me. I wouldn't put all your eggs into this basket because you don't have to make it much more complicated to run into limitations with preprocessor syntax but, as it is, this is fine.