-1

I have a string function written in rtstring.c file which simply accept a string and return back the same string. Then i have included this file in a body.cpp file and i want to pass string from this file to c file. How can I do this please help

My code

rtstring.c

#include <stdio.h>
char * rtstr(char * s)
{ 
    return s;
}

body.cpp

#pragma once 
#include "header.h"
#include "rtstring.c" 
mystring::mystring(string str)
{
    st=str;
}   
string mystring::strreturn(string s1)
{ 
    st=rtstr(s1);
    return st;
}

header.h

#pragma once
class mystring
{
public:
    mystring(string a);
    string strreturn(string s1);
private:
    string st;
}

main.cpp

#include <iostream> 
using namespace std;
#include "header.h"
int main()
{
    string rs,s="hi hello";
    mystring str1(s);
    rs=str.strreturn(s);
    cout<<"string from c"<<rs;
    return 0;
}

I an getting an error of return type mismatch and many associated error. Please help if there is anyway to deal with it. an example will be helpfull. Thanks in advance

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
nikhil
  • 1
  • 7
  • 2
    Please format your code properly and indicate where the error occurs, and the exact error message. In any case: I see std::string being passed to a function taking char*, there's no such conversion (assuming `string` is std::string, hard to tell). Can't you use const char* btw? Also what is mystring supposed to do? Take copies of std::string? You don't need a separate class for that. – stijn May 29 '17 at 07:32
  • A C++ string is different from a C string which is only a char array – 97amarnathk May 29 '17 at 07:35
  • 1
    I'm horribly confused about what you are going to do with this. `std::string` contains all functionality to deal with C-style strings and their copies. – MABVT May 29 '17 at 07:35
  • Use c_str() to get C style string. [C++ Reference](http://www.cplusplus.com/reference/string/string/c_str/) – Pravin May 29 '17 at 07:37
  • I,m trying to create a c++ wraper for c which will be a dll to c# – nikhil May 29 '17 at 07:38
  • If you want to interop between C++ and C#, consider a C++/CLI dll in between instead of a C dll. C++/CLI already has builtin functions for conversion between System.String and std::string so you don't need any of this code shown here. And likely less bugs as well. – stijn May 29 '17 at 07:53
  • stijin can u provide me any link for the following? – nikhil May 29 '17 at 08:39
  • Your included `.c` file is compiled as C++, not C! If you want C, compile it seperately and link to your C project. As-is, it is all C++. And a function is not a "program". From your comments it looks like you try to shoot your foot the most complicated way. – too honest for this site May 29 '17 at 09:27
  • olaf i have found a way out as per my need i have updated the change as an answer to my own post – nikhil May 29 '17 at 10:37
  • .c file can be included without compiling. At the end everything is c – nikhil May 29 '17 at 10:39
  • 1
    Here's a pretty good example wrapping some C++ code to be called from C#: https://stackoverflow.com/questions/19163839/how-to-access-class-in-c-cli-from-c – stijn May 29 '17 at 11:18

4 Answers4

2

I an getting an error of return type mismatch

If you were to read the complete error message, I'm sure it would tell you that this is the problem:

char * rtstr(char * s)
//           ^^^^^^

string mystring::strreturn(string s1)
//                         ^^^^^^
{ 
    st=rtstr(s1);
//           ^^

You are trying to pass a std::string into a function that instead expects char*.

To solve the problem, don't try to pass a std::string to functions that expect a char*. You'll probably find std::string::c_str() member function useful.


#include "rtstring.c" 

Don't include source files. Especially don't include source files that were written in another language.

To call a function defined in another source file, just declare the function in the calling source and link the object file of the other source. Remember to set the language linkage when declaring cross-language functions. You'll find header files useful for including function declarations.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Your code is not much clear to me, but I understand your question.

So, accordingly, one of the simplest way is to write the string into a file using C programming and then reading the same file in your cpp program.

Your C program to write into a file:

#include <stdio.h>
#include <stdlib.h>  /* For exit() function */
int main()
{
   char s[1000];
   FILE *fptr;

   fptr = fopen("program.txt", "w");
   if(fptr == NULL)
   {
      printf("Error!");
      exit(1);
   }

   printf("Enter your string:\n");
   gets(s);

   fprintf(fptr,"%s", s);
   fclose(fptr);

   return 0;
}

Your cpp program something like this to read the same file.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
              char c,fname[20] = 'program.txt';
              clrscr();
              ifstream in(fname);
              if(!in)
              {
                            cout<<"File Does not Exist";
                            getch();
                            return;
              }
              cout<<"\n\n";
              while(in.eof()==0)
              {
                            in.get(c);
                            cout<<c;
              }
              getch();
}

Another option could be to use pipe() function. More details.

Bidisha Pyne
  • 543
  • 2
  • 13
0

I have figured it out a way which suits my requirement best. I appreciate all your help thanks. Changes i have done is as follows

body.cpp `

string mystring::strreturn(string s1)
{
char *p,arr[9];
char *c=new char[s1.size()+1];
copy(s1.begin(),s1.end(),c);
c[s1.size()]='\0';
p=rtstr(c);
strcpy(arr,p);
st=arr;
return st;
}

` please insert appropriate headers. "using namespace std;" both in header.h and body.cpp "#include" in header.h

nikhil
  • 1
  • 7
-1

First of all, you don't include source files. You should make rtstring.c it's own header, in which you declare the function rtstr.

Second, you're using a C++ compiler. If you want it to emulate C, you use using "C" { ... }. There are still fundamental differences between proper C and C++, so you're still using a C subset of C++, not actual C.

And finally, if you want to convert an std::string to a C string (char*), you can use its c_str() function.

Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48