c ++编译中的错误

时间:2014-04-09 00:14:28

标签: c++ compilation g++

好的,所以我试图在linux服务器上编译学校项目,但它给我带来了错误。我使用的编译器是g ++,&为了记录这个程序在我上传到这个服务器之前在我的Mac上工作正常。基本上,我只是不知道如何阅读此错误消息,因此我不知道要修复的内容,修复的位置或您需要查看的代码。如果有人可以请告诉我在这里发布什么,所以你们可以进一步推断你会很棒...哦,我的项目将在今晚11点到期:D哈哈反正,下面是我键入的命令然后消息它向我吐口水。

g++ Song.cpp Song.h test_tsuPod.cpp tsuPod.h tsuPod.cpp

现在错误......为一些胡言乱语做好准备

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ios:39,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:40,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iostream:40,
                 from test_tsuPod.cpp:9:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ios_base.h:790: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd:47: error: within this context
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd: In copy constructor ‘std::basic_fstream<char, std::char_traits<char> >::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd:87: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here 
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/streambuf: In copy constructor ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/streambuf:770: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd:78: error: within this context
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd: In copy constructor ‘std::basic_fstream<char, std::char_traits<char> >::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd:87: note: synthesized method ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’ first required here 
In file included from test_tsuPod.cpp:10:
tsuPod.h: In copy constructor ‘tsuPod::tsuPod(const tsuPod&)’:
tsuPod.h:23: note: synthesized method ‘std::basic_fstream<char, std::char_traits<char> >::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&)’ first required here 
test_tsuPod.cpp: In function ‘int main()’:
test_tsuPod.cpp:23: note: synthesized method ‘tsuPod::tsuPod(const tsuPod&)’ first required here 

如果你能救我,我会永远欠你的债!谢谢!

以下是tsuPod.h的代码

#ifndef __project5__tsuPod__
#define __project5__tsuPod__

#include <iostream>

#endif /* defined(__project5__tsuPod__) */

#include "Song.h"
#include <fstream>


using namespace std;


class tsuPod {
  private:
  int MAX_SONGS;
  int MAX_MEM;
  int num_songs;
  int memory;
  fstream file;


  public:
  int getNumSongs();
  int getMemoryUsage();
  int addSong(string, string, int);
  int deleteSong(string);
  int shuffle();
  int clearSongList();
  int showSongList();
  int sortSongList();
  int getTotalMemory();
  int getRemainingMemory();

  //constructor
  tsuPod(int, int);


};

所以你可以看到,这是我的test_tsuPod.cpp,它是这个程序的驱动程序

#include <iostream> // here is line # 9, as compiler says something about this line
#include "tsuPod.h"
#include <fstream>


using namespace std;

int main()
{
  const int MAX_SONGS = 25;
  const int MAX_MEMORY = 100;



  tsuPod TsuPodApp = tsuPod(MAX_SONGS, MAX_MEMORY);

  cout << "Total memory : " << TsuPodApp.getTotalMemory() << endl;

  TsuPodApp.addSong("Something", "artist", 80);
  TsuPodApp.addSong("2", "artist num 2", 10);

  cout << "Remaining Memory : " << TsuPodApp.getRemainingMemory() << endl;

  TsuPodApp.addSong("3", "artist num 3", 1);
  TsuPodApp.addSong("4", "artist num 4", 1);
  TsuPodApp.addSong("5", "Other artist", 10); // should trigger memory error


  TsuPodApp.showSongList();

  TsuPodApp.deleteSong("2");

  TsuPodApp.showSongList();

  cout << "Remaining Memory : " << TsuPodApp.getRemainingMemory() << endl;

  TsuPodApp.sortSongList();

  TsuPodApp.showSongList();

  TsuPodApp.clearSongList();

  TsuPodApp.showSongList();


  return 0;
}

真棒!看来你们修复了我所遇到的警察构造函数错误!但是现在当我尝试编译时,我收到了这条消息 - 这很奇怪,因为我肯定这些函数是定义的。有什么想法吗?

/tmp/ccu7S45x.o: In function `tsuPod::addSong(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
tsuPod.cpp:(.text+0x20d): undefined reference to `Song::Song(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
tsuPod.cpp:(.text+0x28d): undefined reference to `Song::getTitle()'
tsuPod.cpp:(.text+0x2f6): undefined reference to `Song::getArtist()'
collect2: ld returned 1 exit status

好的,这里要求的是Song.cpp - 这是一篇很长的帖子哈哈

#include "Song.h"



string Song::getTitle()
{
  return title;
}

string Song::getArtist()
{
  return artist;
}

int Song::getSize()
{
  return size;
}

int Song::setTitle(string some)
{
  if(some.length() == 0)
  {
    cout << "To add a song, it has to have a title."
    << endl;
    return 0;
  }

  title = some;
  return 1;
}

int Song::setArtist(string some)
{
  if(some.length() == 0)
  {
    cout << "To add a song, it has to have an artist name."
    << endl;
    return 0;
  }

  artist = some;
  return 1;
}

int Song::setSize(int some)
{
  if(some <= 0)
  {
    cout << "To add a song, it has to have minimum size of 1 mb." << endl;
    return 0;
  }

  size = some;
  return 1;
}

Song::Song(string song_title, string song_artist, int song_size)
{
  if(song_title.length() == 0 || song_artist.length() == 0)
  {
    cout << "To add a song, it has to have a name and artist name."
    << endl;
    return;
  }
  if(song_size <= 0)
  {
    cout << "To add a song, it has to have minimum size of 1 mb." << endl;
    return;
  }
  title  = song_title;
  artist = song_artist;
  size   = song_size;
}

下面的song.h

#ifndef __project5__Song__
#define __project5__Song__

#include <iostream>
#include <string>

using namespace std;

#endif /* defined(__project5__Song__) */


class Song {
  private:
    string title;
    string artist;
    int size;
  public:
    string getTitle();
    string getArtist();
    int getSize();
    int setTitle(string);
    int setArtist(string);
    int setSize(int);

  // constructor
  Song(string, string, int);

};

3 个答案:

答案 0 :(得分:2)

编译器正在尝试为tsuPod发出隐式复制构造函数,但由于fstream成员不可复制,因此无法执行此操作。您正试图在test_tsuPod.cpp:23复制您的课程。

答案 1 :(得分:1)

导致问题的一行是:

tsuPod TsuPodApp = tsuPod(MAX_SONGS, MAX_MEMORY);

此行正在从临时值构建TsuPodApp。发生这种情况时,编译器将尝试将临时副本复制到TsuPodApp。但为了实现这一点,类tsuPod必须具有复制构造函数。因为std::fstream具有私有拷贝构造函数(在C ++ 11中删除),所以无法复制该类,因此错误。

解决方案是直接构造TsuPodApp,如下所示:

tsuPod TsuPodApp(MAX_SONGS, MAX_MEMORY);

此处将调用的唯一构造函数是您定义的构造函数(tsuPod(int, int))。

答案 2 :(得分:1)

变化:

tsuPod TsuPodApp = tsuPod(MAX_SONGS, MAX_MEMORY);

为:

tsuPod TsuPodApp(MAX_SONGS, MAX_MEMORY);

g ++ a.cc a.h -o a.o

g ++ b.cc b.h a.o -o myexe

等。