1

これが私が使っているコードです。new_flight空の宣言以外のコードが現在ないこの関数にすべての入力を渡せるようにしたいと思います。参照によってトークンを渡そうとしていますが* &、値だけでトークンを渡そうとしましたが、どれも機能していないようです。

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <iterator>

using namespace std;

void new_flight( vector<string> &tokens );

int main( int argc, char *argv[] )
{
    vector<string> tokens;

    cout << "Reservations >> ";
    getline(cin, input);
    istringstream iss( input );
    copy(istream_iterator<string>( iss ),
         istream_iterator<string>(),
         back_inserter<vector<string> > ( tokens ));

    new_flight( tokens );
}

これがコンパイラが私に言っていることです

Undefined symbols for architecture x86_64:
  "new_flight(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)", referenced from:
      _main in ccplPBEo.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

また、実際にトークンをnew_flightに渡す行をコメントアウトすると、正常new_flight( tokens )にコンパイルされます。

ご覧いただきありがとうございます

4

5 に答える 5

3

関数をスタブアウトするには、関数宣言ではなく、関数定義を提供する必要があります。

void new_flight( vector<string> &tokens ) {
    // Not implemented
}
于 2013-01-28T00:27:08.737 に答える
2

発生しているのはコンパイラエラーではなく、リンカnew_flight()エラーです。これは、関数が定義されていないことが原因です。しかし、あなたはこの事実に気づいているようです。定義されていない関数を呼び出すと、プログラムが機能することを期待できないため、リンカはそもそもプログラムの作成を拒否します。

于 2013-01-28T00:26:21.260 に答える
2

関数を宣言していますnew_flightが、定義していないため、リンカは関数をリンクできません。実装(スタブのみの場合)を作成すると、コンパイルされます。

于 2013-01-28T00:27:00.250 に答える
1

宣言を呼び出すことはできません。定義が必要です。コンパイラはこの呼び出しのためにどのコードを生成することになっていますか?関数のコードはありません。プログラムをコンパイルできません。

于 2013-01-28T00:26:30.900 に答える
0

他の投稿が指摘しているように:

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <iterator>

using namespace std;

void new_flight( vector<string> &tokens );

int main( int argc, char *argv[] )
{
   vector<string> tokens;

   cout << "Reservations >> ";
   getline(cin, input);
   istringstream iss( input );
   copy(istream_iterator<string>( iss ),
     istream_iterator<string>(),
     back_inserter<vector<string> > ( tokens ));

   new_flight( tokens );
}

void new_flight( vector<string> &tokens ) 
{

   // implementation
}

基本的にmainの後に機能を定義しているので、コンパイラは関数が存在することを知る必要があります。したがって、void new_flight( vector<string> &tokens );関数を定義する「プロトタイプ」を作成します。

于 2013-01-28T00:29:24.890 に答える