LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problems with "error: invalid conversion from `const char*' to `char*' " (https://www.linuxquestions.org/questions/linux-newbie-8/problems-with-error-invalid-conversion-from-%60const-char%2A-to-%60char%2A-684132/)

dave.f.one 11-17-2008 02:39 PM

Problems with "error: invalid conversion from `const char*' to `char*' "
 
Hi, I need some help. I tried to install obs-ns. Itīs based on ns2, but when I ran "make", the problem below appears... please I wish anybody would help me.


This is the error:

Code:

OBS/ns-2/integrated_agent.h: In constructor `BurstManager::BurstManager()':
OBS/ns-2/integrated_agent.h:86: warning: `BurstManager::bt_' will be initialized after
OBS/ns-2/integrated_agent.h:84: warning:  `int BurstManager::currburstsize_'
OBS/ns-2/integrated_agent.cc:66: warning:  when initialized here
OBS/ns-2/integrated_agent.cc: In member function `int BurstManager::nhops(nsaddr_t, nsaddr_t)':
OBS/ns-2/integrated_agent.cc:81: error: invalid conversion from `const char*' to `char*'  <------- this is the error
OBS/ns-2/integrated_agent.cc: In member function `virtual void IPKTAgent::recv(Packet*, Handler*)':
OBS/ns-2/integrated_agent.cc:180: warning: unused variable 'ipkth'
/root/OBS/ns-allinone-2.31/tclcl-1.19/tclcl.h:150: error: `void Tcl::error(const char*)' is private
OBS/ns-2/integrated_agent.cc:185: error: within this context
OBS/ns-2/integrated_agent.cc:155: warning: unused variable 'hdr'
OBS/ns-2/integrated_agent.cc: In member function `void IPKTAgent::deBurst(Packet*)':
OBS/ns-2/integrated_agent.cc:192: warning: unused variable 'hdrip'
OBS/ns-2/integrated_agent.cc:194: warning: unused variable 'ch'
make: *** [OBS/ns-2/integrated_agent.o] Error 1


And this is the code of the program...


Code:

int BurstManager::nhops(nsaddr_t src,nsaddr_t des) {
  Tcl& tcl = Tcl::instance();
  sprintf(tcl.buffer(),"$ns nhops %d %d",src,des);
  tcl.eval();
  char *ni = tcl.result();  <------this is the line 81
  return atoi(ni);
}


i92guboj 11-17-2008 02:45 PM

Look for the declaration (it should be char *ni; or a similar thing at the top of the function or file. If it has some global significance it might be defined in the matching .h file instead of the .c file. The cause of the problem might lie in that declaration.

johnsfine 11-17-2008 05:25 PM

Quote:

Originally Posted by dave.f.one (Post 3345289)
Hi, I need some help. I tried to install obs-ns. Itīs based on ns2, but when I ran "make", the problem below appears... please I wish anybody would help me.

Maybe this means some older compiler didn't mind that the code was wrong. (The alternative theory would be a bigger problem: obs-ns might be based on some earlier, possibly incompatible, version of the Tcl header files).

But that error looks easy enough to correct, so try correcting it. That might just move down to the next error. But it's worth a try.

Quote:

Code:

int BurstManager::nhops(nsaddr_t src,nsaddr_t des) {
  Tcl& tcl = Tcl::instance();
  sprintf(tcl.buffer(),"$ns nhops %d %d",src,des);
  tcl.eval();
  char *ni = tcl.result();  <------this is the line 81
  return atoi(ni);
}


just add "const" where required:

Code:

int BurstManager::nhops(nsaddr_t src,nsaddr_t des) {
  Tcl& tcl = Tcl::instance();
  sprintf(tcl.buffer(),"$ns nhops %d %d",src,des);
  tcl.eval();
  const char *ni = tcl.result();
  return atoi(ni);
}

This is NOT a generic fix for other places you get that same error message. It works here only because of the way ni is used before it goes out of scope.

I greatly prefer
char const* ni = tcl.result();
rather than
const char *ni = tcl.result();
but they mean the same thing and the latter should be more obvious to a C++ beginner from the original code plus the error message.

EmrldDrgn 11-17-2008 06:13 PM

You might also want to check and see if there is a newer version of the package, or if a fix for your problem already exists. It is very rare that you're the only one to ever see a problem like that. Just my 2c.

maschiltz 11-17-2008 09:14 PM

Rather than try and debug the code, I would just check the documentation and verify the software requirements and make sure your versions all match up.

Good Luck
Mark Schiltz

maschiltz 11-17-2008 09:16 PM

Rather than try and debug the code, I would just check the documentation and verify the software requirements and make sure your versions all match up.

Good Luck
Mark Schiltz

dave.f.one 11-17-2008 11:06 PM

Thanks guys
I use Fedora Core 3 and this software (obs-ns) was developed in 2000 or 2001. I didn't find a new version. I try to install in Fedora 9 and in Ubuntu 7.1 but it doesnīt work. Additionally, I use ns-allinone-2.31 with the most recently packages of all. I try this:

char const *ni = tcl.result();

And this work, but I have more similar problems.

Thanks for all, but I have more problems.

i92guboj 11-18-2008 02:09 AM

Then yoy either need to
  • fix all
  • change g++ version, probably to an older one, check the docs
  • look for patches that allow it to compile under newer compilers

This is a relatively frequent problem because of bad programming practices sometimes. Each version of the compiler is, in general, stricter than the previous one in which regards the acknowledgment of the standads.

dave.f.one 11-18-2008 10:47 AM

Another problem with obs-ns
 
Hi again,

I resolved some problems but I have another problem:

Code:

OBS/ns-2/stats.cc: In member function `virtual int StatCollector::command(int, const char* const*)':
OBS/ns-2/stats.cc:59: warning: unused variable 'tcl'
OBS/ns-2/stats.cc: In member function `void StatCollector::dumpstats(const char*, float, float, int)':
OBS/ns-2/stats.cc:81: error: `ofstream' undeclared (first use this function)  <------------this error
OBS/ns-2/stats.cc:81: error: (Each undeclared identifier is reported only once for each function it appears in.)
OBS/ns-2/stats.cc:81: error: expected `;' before "outfile1"
OBS/ns-2/stats.cc:82: error: expected `;' before "outfile2"
OBS/ns-2/stats.cc:84: error: `outfile1' undeclared (first use this function)
OBS/ns-2/stats.cc:84: error: `outfile2' undeclared (first use this function)
OBS/ns-2/stats.cc:85: error: `cerr' undeclared (first use this function)
OBS/ns-2/stats.cc:85: error: `endl' undeclared (first use this function)
make: *** [OBS/ns-2/stats.o] Error 1


johnsfine 11-18-2008 02:40 PM

I don't think you mentioned which version of gcc you're using.

One detail became way pickier, (I think in version 4.3): You need the correct #include directives.

In earlier versions, some include files brought in other definitions they weren't supposed to, so things like your example ofstream might be defined even thought the right #include isn't present.

So you just need to add the right #include near the top of the .cc file. But I never learned how you're supposed to look up which is the right #include for a specific function. (Any experts here what to tell me that one?)

My guess is the line you need to add is

#include <fstream>

dave.f.one 11-19-2008 10:01 AM

I tried with

#include <fstream>

But it doesnīt work

The version of gcc is 3.4.2

this is the code:

Code:

void StatCollector::dumpstats(const char *filename,const float parameter, float simtime,int nagents) {
  char f1name[30],f2name[30]; // one collects stats, one values for the graph

  strcpy(f1name,filename);
  strcpy(f2name,filename);

  strcat(f1name,".stat");
  strcat(f2name,".plot");
 
  ofstream outfile1(f1name,ios ::app);  <------------ this error
  ofstream outfile2(f2name,ios::app);


Pinakini 02-11-2015 07:17 AM

i am facing the same problem..will you help me??


All times are GMT -5. The time now is 06:33 PM.