0

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?`

1 Answers1

2

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    better use `std::ofstream` when the stream is used for writing only – 463035818_is_not_an_ai Sep 06 '22 at 07:21
  • Thank @john for ur response, I rewrite the actual code as u can see above, please can u help me why this error occur? – بسمة احمد Sep 06 '22 at 08:32
  • @بسمةاحمد See the edit to my answer. – john Sep 06 '22 at 08:43
  • @john the posted problem has been sloved but another error occured:syntax error near unexpected token `(' line 14: ` void callhandler(const nav_msgs::Odometry::ConstPtr& msg) – بسمة احمد Sep 06 '22 at 08:50
  • @بسمةاحمد Well if you want to include the latest code and error I'll do my best to suggest a fix. – john Sep 06 '22 at 08:52
  • @بسمةاحمد There's something strange happening. Basically as soon as the header files are finished but the code starts you get an error. I don't know what might be causing that. – john Sep 06 '22 at 08:53
  • @بسمةاحمد It seems like one of your header files has a bug in it. But that seems quite unlikely. I think this is not a C++ problem, but something wrong in your environment or your tools. I'm afraid I can't really help you with that. – john Sep 06 '22 at 09:00
  • @john, hi the problem has been sloved I was running the code as: rosrun pkg_name node.cpp which is wrong the correct way to run is: rosrun pkg_name node – بسمة احمد Sep 07 '22 at 09:45