我想从一个“主”文件中的另一个文件包含一个函数。我遵循这种模式:简单的C++函数包含失败
http://www.learncpp.com/cpp-tutorial/18-programs-with-multiple-files/
这里是我的主文件,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;
}
这里是我的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;
}
我在Ubuntu12.10米编译使用G ++编译器像这样:
g++ digispark.cpp send.cpp -o digispark
它编译成功。
然而,当我运行程序,“你好”不上来。所以我不相信这个函数被调用。我究竟做错了什么?任何帮助将是伟大的!谢谢!
编辑:
如何处理这个问题:
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;
}
对于那些你们谁是困惑,为什么我坚持这样做,正如我以前说过,发送功能已建成接受char ** argv(或char * argv [])。我的观点是试图模仿我的主要功能。
这本来是更难以改写,其实云在发送功能采取不同的类型参数不仅仅是在它所想要发送的功能。感谢大家!
因此,如果这有助于任何人试图类似的东西随意使用吧!
不使用文件stdio.h和string.h中,但cstdio并为c_string在C++ – hetepeperfan
这是一个问题与约定或将实际上打破我的计划? – eatonphil
您可能没有准备好使用指针。正如你的代码所显示的那样,它们很难并且很容易被滥用。尽可能地坚持标准的C++库构造。 –