1

我编写了一个程序来根据从输入文件中读取的数据创建 gnuplot 命令。我需要太多命令才能手动执行此操作。这工作得很好,但我现在需要从两个不同的数据文件中读取数据并使用它们来创建 gnuplot 命令。不幸的是,现在读取这两个文件似乎出了点问题。从数据文件中读取涉及的代码部分如下:

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int n_snapshots;

int main () {

    std::cout << "Enter number of snapshots" << "\n";
    std::cin >> n_snapshots;

    int snap_cell_count[n_snapshots];

    std::ifstream in_file_count("data/snapshot_data");
    std::ifstream in_file_fates("data/cell_fates_data_final");

    for (int i=0; i<n_snapshots; i++) {
        in_file_count >> snap_cell_count[i];
    }
    in_file_count.close();

    int cell_fates[snap_cell_count[n_snapshots-1]];
    for (int i=0; i<snap_cell_count[n_snapshots-1]; i++) {
        in_file_fates >> cell_fates[i];
    }
    in_file_fates.close(); 

n_snapshots 只是一些整数,snap_cell_count[] 是一个包含“n_shapshots”元素的数组,每个元素都从数据文件“snapshot_data”中读取一个值。cell_fates[] 是一个数组,其元素数量等于 snap_cell_count[] 中最后一个元素的值,并且再次从文件中读取其元素的值,在本例中为“cell_fates_data_final”。要读取的数据文件存储在名为“data”的文件夹中。

不幸的是,编译器返回以下错误。

Undefined symbols for architecture x86_64:
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const", referenced from:
  _main in ccrpSAv5.o
  "std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::str() const", referenced from:
  _main in ccrpSAv5.o
  "std::basic_istream<char, std::char_traits<char> >::operator>>(int&)", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
  _main in ccrpSAv5.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ifstream<char, std::char_traits<char> >::close()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ofstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ofstream<char, std::char_traits<char> >::close()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ofstream<char, std::char_traits<char> >::~basic_ofstream()", referenced from:
  _main in ccrpSAv5.o
  "std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::_Ios_Openmode)", referenced from:
  _main in ccrpSAv5.o
  "std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream()", referenced from:
  _main in ccrpSAv5.o
  "std::ios_base::Init::Init()", referenced from:
  __static_initialization_and_destruction_0(int, int) in ccrpSAv5.o
  "std::ios_base::Init::~Init()", referenced from:
  __static_initialization_and_destruction_0(int, int) in ccrpSAv5.o
  "std::cin", referenced from:
  _main in ccrpSAv5.o
  "std::cout", referenced from:
  _main in ccrpSAv5.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
  _main in ccrpSAv5.o
  "___gxx_personality_v0", referenced from:
  Dwarf Exception Unwind Info (__eh_frame) in ccrpSAv5.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

这是使用 Mac OSX Mountain Lion 上的 gcc 编译器编译的。

有谁知道这里有什么问题?

4

1 回答 1

2

好吧,对于初学者来说,您不能只说在运行时确定int snap_cell_count[n_snapshots]的价值。n_snapshots你必须做类似的事情

int *snap_cell_count = new int[n_snapshots];
// do some stuff
delete[] snap_cell_count;

同样的事情cell_fates

至于您的链接器错误,也许这个问题可以解决您的问题......

于 2013-08-08T10:56:07.687 回答