我想從一個「主」文件中的另一個文件包含一個函數。我遵循這種模式:簡單的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++庫構造。 –