collect2:ld返回1退出状态错误

时间:2013-10-09 03:57:26

标签: c++ header fstream

我正在编写基本的C ++代码来练习使用fstream和头文件。我一直收到以下错误:

  

initializer.cpp :(。text + 0xd9):未定义引用`DataSet :: DataSet(int,std :: basic_string,std :: allocator>,std :: basic_string,std :: allocator>,double )”   collect2:ld返回1退出状态

这是主要的:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "dataset.h"
using namespace std;

int main()
{
  ofstream outData("data.txt",ios::binary);

  if(!outData)
  {
    cerr<<"\nFile could not be opened!\n";
    exit(1);
  }

  DataSet blankData;

  for(int i=0;i<100;i++)
    outData.write(reinterpret_cast<const char *>(&blankData),
      sizeof(DataSet));

  return 0;
}

和标题:

#ifndef DATASET_H
#define DATASET_H

#include <string>
using namespace std;

class DataSet
{
public:
  DataSet(int=0,string="",string="",double=0.0);

  void setAccountNumber(int);
  int getAccountNumber() const;

  void setLastName(string);
  string getLastName() const;

  void setFirstName(string);
  string getFirstName() const;

  void setBalance(double);
  double getBalance() const;
private:
  int accountNumber;
  char lastName[15];
  char firstName[10];
  double balance;
};

#endif

和相应的.cpp:

#include <string>
#include "dataset.h"

DataSet::DataSet(int accountNumberValue,
  string lastNameValue,string firstNameValue,
  double balanceValue)
{
  setAccountNumber(accountNumberValue);
  setLastName(lastNameValue);
  setFirstName(firstNameValue);
  setBalance(balanceValue);
}

int DataSet::setAccountNumber(int accountNumberValue)
{
  accountNumber=accountNumberValue;
}

void DataSet::getAccountNumber() const
{
  return accountNumber;
}

string DataSet::setLastName(string lastNameString)
{
  const char *lastNameValue=lastNameString.data();
  int length=lastNameString.size();
  length=(length<15?length:14);
  strncpy(lastName,lastNameValue,length);
  lastName[length]='\0';
}

void DataSet::getLastName() const
{
  return lastName;
}

string DataSet::setFirstName(string firstNameString)
{
  const char *firstNameValue=firstNameString.data();
  int length=firstNameString.size();
  length=(length<10?length:9);
  strncpy(firstName,firstNameValue,length);
  firstName[length]='\0';
}

void DataSet::getFirstName() const
{
  return firstName;
}

double DataSet::setBalance(double balanceValue)
{
  balance=balanceValue;
}

void DataSet::getBalance() const
{
  return balance;
}

1 个答案:

答案 0 :(得分:1)

链接器未找到Initializer对象中使用的“DataSet :: DataSet()”符号。 如果为相应的cpp生成的对象文件没有与Initializer链接在一起,可能会发生这种情况,有时可能会因为您没有将相应的cpp添加到project / makefile而发生。