Domanda

I am trying to include a function from another file inside a "main" file. I'm following this paradigm:

http://www.learncpp.com/cpp-tutorial/18-programs-with-multiple-files/

Here is my main file, digispark.cpp:

#include <iostream>

using namespace std;

int send(int argc, char **argv);

int main()
{
    char* on;
    *on = '1';
    char* off;
    *off = '0';
    send(1,&on);
    return 0;
}

And here is my send.cpp:

#include <stdio.h>
#include <iostream>
#include <string.h>
#if defined WIN
    #include <lusb0_usb.h>    // this is libusb, see http://libusb.sourceforge.net/
#else
    #include <usb.h>        // this is libusb, see http://libusb.sourceforge.net/
#endif

// I've simplified the contents of send for my debugging and your aid, but the
// complicated arguments are a part of the function that will eventually need
// to be here.
int send (int argc, char **argv)
{

    std::cout << "Hello";
    return 0;
}

I'm compiling on Ubuntu 12.10 using the g++ compiler like so:

g++ digispark.cpp send.cpp -o digispark

It compiles successfully.

However, when I run the program, "Hello" does not come up. Therefore I don't believe the function is being called at all. What am I doing wrong? Any help would be great! Thanks!

EDIT:

How I dealt with the issue:

int send(int argc, char **argv);

int main()
{
    char* on[4];
    on[0] = (char*)"send";
    on[1] = (char*)"1";
    char* off[4];
    off[0] = (char*)"send";
    off[1] = (char*)"0";  
    send(2,on);
    return 0;
}

For those of you who were confused as to why I insisted doing this, as I said before, the send function was already built to accept the char** argv (or char* argv[]). My point was to try to mimic that in my main function.

It would have been much more difficult to rewrite the function that actually goes in the send function to take a different type of argument than just to send in what it wanted. Thanks everyone!

So if this helps anyone trying something similar feel free to use it!

È stato utile?

Soluzione

Your problem is not the one you think it is. It's here:

char* on;
*on = '1';

You declared a char pointer, but did not initialize it. Then you dereferenced it. Bang, you're dead. This is what is known as Undefined Behavior. Once you invoke U.B., anything can happen. If you're lucky, it's a crash. But I guess you weren't lucky this time.

Look, if you want to start storing things in memory, you have to allocate that memory first. The best way, as hetepeperfan said, is to just use std::string and let that class take care of all the allocating/deallocating for you. But if for some reason you think you have to use C-style strings and pointers, then try this:

char on[128]; //or however much room you think you'll need. Don't know? Maybe you shoulda used std::string ...
*on = '1';
*(on+1) = '\0'; //if you're using C-strings, better null terminate.
char off[128];
*off = '0';
*(off+1) = '\0';
send(1,&on);

Altri suggerimenti

ok I think you try to do something like the following, I tried to make it a bit more in the Style of C++ and prevent the use of pointers since they should not be necessary in the code that you showed.

digispark.cpp

#include "send.h"

int main (int argc, char** argv){

    string on  = "1";
    string off = "0";

    send ( on );
    send ( off );

    return 0;
}

send.cpp

#include <iostream>
#include <string>

void send( const std::string& s) {

    std::cout << s << std::endl;

}

send.h

void send(const std::string& s);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top