越来越多的指针错误:分配中的类型不兼容

时间:2017-10-30 16:15:17

标签: c++ arrays pointers

我正在进行任务,我有这个错误,这对我没有意义。基本上我们有“Playlist”类,它有一系列指向“Track”的指针

class Playlist{
private:
    string playlistName;
    int trackCount;
    Track*  tracks[0];
public:
    Playlist(string name);
    Track* GetTrack(int index);
    int GetTrackCount();
    void AddTrack(Track* track, int index);
    void AddTrack(Track* track);
    void RemoveTrack(int index);
    void RemoveTrack(Track* track);
};

错误发生在AddTrack(Track t,int i)和RemoveTrack(int i)函数中

    Playlist.cpp: In member function ‘void Playlist::AddTrack(Track*, int)’:
Playlist.cpp:27:11: error: incompatible types in assignment of ‘Track* [(((Playlist*)this)->Playlist::trackCount + 1)]’ to ‘Track* [1]’
     tracks= newtracks;
           ^
Playlist.cpp: In member function ‘void Playlist::RemoveTrack(int)’:
Playlist.cpp:42:12: error: incompatible types in assignment of ‘Track* [(((Playlist*)this)->Playlist::trackCount + -1)]’ to ‘Track* [1]’
     tracks = newtracks;
            ^
student@ldnel:~/Documents/A2$ g++ -std=c++14 *.cpp
Playlist.cpp: In member function ‘void Playlist::AddTrack(Track*, int)’:
Playlist.cpp:28:11: error: incompatible types in assignment of ‘Track* [newSize]’ to ‘Track* [1]’
     tracks= newtracks;
           ^
Playlist.cpp: In member function ‘void Playlist::RemoveTrack(int)’:
Playlist.cpp:44:12: error: incompatible types in assignment of ‘Track* [newSize]’ to ‘Track* [1]’
     tracks = newtracks;
            ^

这是我的函数实现:

Playlist::Playlist(string name){
    playlistName=name;
}
Track* Playlist::GetTrack(int index){

    return tracks[index];
}
int Playlist::GetTrackCount(){
    return trackCount;
}
void Playlist::AddTrack(Track* track, int index){
    int newSize = trackCount+1;
    Track  *newtracks[newSize];
    for(int i=0;i<newSize;i++){
        if(i<index){
            newtracks[i] = tracks[i];
        }else if(i==index){
            newtracks[i]=track;
        }else{
            newtracks[i]=tracks[i-1];
        }
    }
    tracks= newtracks;
    trackCount++;
}
void Playlist::AddTrack(Track* track){
    AddTrack(track,trackCount);
}
void Playlist::RemoveTrack(int index){
    int newSize = trackCount-1;
    Track *newtracks[newSize];
    for(int i=0;i<newSize;i++){
        if(i<index){
            newtracks[i]=tracks[i];
        }else{
            newtracks[i]=tracks[i+1];
        }
    }
    tracks = newtracks;
    trackCount--;
}
void Playlist::RemoveTrack(Track* track){
    for(int i=0;i<trackCount;i++){
        if(tracks[i]==track){
            RemoveTrack(i);
            return;
        }
    }
}

我打算做的是拥有一个动态存储所有曲目的数据管理器类,并且播放列表会引用它所包含的曲目。我不知道为什么它会考虑两种不同类型的数组

1 个答案:

答案 0 :(得分:2)

您正尝试将长度为x的数组分配给不同大小的数组。我建议你使用std :: vector(http://en.cppreference.com/w/cpp/container/vector)而不是数组。

您可能还想考虑使用std :: shared指针(http://en.cppreference.com/w/cpp/memory/shared_ptr)。

您可能还需要查看AddTrack(Track * track,int index)函数的迭代器(http://en.cppreference.com/w/cpp/iterator/iterator)。

我会像这样实现它:

class Playlist {
private:
    std::string playlistName;
    int trackCount;
    std::vector<std::shared_ptr<Track>> tracks;
public:
    Playlist(std::string name);
    std::shared_ptr<Track> GetTrack(int index);
    int GetTrackCount();
    void AddTrack(std::shared_ptr<Track> track, int index);
    void AddTrack(std::shared_ptr<Track> track);
    void RemoveTrack(int index);
    void RemoveTrack(std::shared_ptr<Track> track);
};

这是我的添加功能:

void Playlist::AddTrack(std::shared_ptr<Track> track, int index) {
    std::vector<std::shared_ptr<Track>>::iterator itor = tracks.begin();
    itor += index;
    tracks.insert(itor, track);
}
void Playlist::AddTrack(std::shared_ptr<Track> track) {
    tracks.push_back(track);
}

我相信你可以自己实施其余的。 :)