复制和粘贴时,我不断出现“线外定义”错误?

时间:2019-02-27 21:58:45

标签: c++ qt

此类是我正在使用QT进行的新C ++项目的model(DBO)类。我开始编码,然后返回创建ERD。今天,我在完成了ERD编码并完成了类定义(标头文件)之后,回到了今天,直到此文件为止,它都定义/实现了CPP文件中的方法。我一直在脱机定义。但是方法是从Header文件复制并粘贴的-我很了解C ++,但是从来没有这个问题,也许那里不应该有一个符号-我有视觉问题,所以可能是某些东西,我看不到赶上。头文件是:

#include "Artist.h"
#include<QDate>
#include<QList>
#include<QString>

#pragma once

namespace net
{
    namespace draconia
    {
        class Media
        {
            unsigned muiMediaId;
            QDate mDtRelease;
            QList<Artist> mLstArtists;
            QString msFilePath, msName;
        protected:
            QList<Artist> getArtistsInternal() const;
            void setArtists(const QList<Artist> &lstArtists);
        public:
            Media();
            Media(const unsigned uiMediaId, const QString &sName, const QDate &dtRelease);
            Media(const unsigned uiMediaId, const QString &sName, const unsigned muiReleaseYear);
            Media(const unsigned uiMediaId, const QString &sName, const QDate &dtRelease, const QString &sFilePath);
            Media(const unsigned uiMediaId, const QString &sName, const unsigned uiReleaseYear, const QString &sFilePath);
            Media(const unsigned uiMediaId, const QString &sName, const QDate &dtRelease, const QList<Artist> &lstArtists);
            Media(const unsigned uiMediaId, const QString &sName, const unsigned uiReleaseYear, const QList<Artist> &lstArtists);
            Media(const unsigned uiMediaId, const QString &sName, const QDate &dtRelease, const QString &sFilePath, const QList<Artist> &lstArtists);
            Media(const unsigned uiMediaId, const QString &sName, const unsigned uiReleaseYear, const QString &sFilePath, const QList<Artist> &lstArtists);
            Media(const Media &refCopy);
            virtual ~Media() = 0;

            void addArtist(const Artist &refArtist);
            const QList<Artist> getArtists() const;
            QString &getFilePath() const;
            unsigned getMediaId() const;
            QString getName() const;
            QDate getReleaseDate() const;
            bool removeArtist(const Artist &refArtist);
            void removeArtist(const unsigned uiIndex);
            void setFilePath(const QString &sFilePath);
            void setMediaId(const unsigned uiMediaId);
            void setName(const QString &sName);
            void setReleaseDate(const QDate &dtRelease);
            void setReleaseDate(const unsigned uiReleaseYear);
        };
    }
}

,CPP文件为:

#include "Media.h"

using namespace net::draconia;

QList<Artist> Media::getArtistsInternal() const
{
    return(mLstArtists);
}

void Media::setArtists(const QList<Artist> &lstArtists)
{
    mLstArtists.clear();
    mLstArtists.append(lstArtists);
}

Media::Media()
    : muiMediaId(0)
{ }

Media::Media(const unsigned uiMediaId, const QString &sName, const QDate &dtRelease)
    : muiMediaId(uiMediaId)
    , mDtRelease(dtRelease)
    , msName(sName)
{ }

Media::Media(const unsigned uiMediaId, const QString &sName, const unsigned uiReleaseYear)
    : muiMediaId(uiMediaId)
    , mDtRelease(QDate(static_cast<int>(uiReleaseYear), 1, 1))
    , msName(sName)
{ }

Media::Media(const unsigned uiMediaId, const QString &sName, const QDate &dtRelease, const QString &sFilePath)
    : muiMediaId(uiMediaId)
    , mDtRelease(dtRelease)
    , msFilePath(sFilePath), msName(sName)
{ }

Media::Media(const unsigned uiMediaId, const QString &sName, const unsigned uiReleaseYear, const QString &sFilePath)
    : muiMediaId(uiMediaId)
    , mDtRelease(QDate(static_cast<int>(uiReleaseYear), 1, 1))
    , msFilePath(sFilePath), msName(sName)
{ }

Media::Media(const unsigned uiMediaId, const QString &sName, const QDate &dtRelease, const QList<Artist> &lstArtists)
    : muiMediaId(uiMediaId)
    , mDtRelease(dtRelease)
    , msName(sName)
{
    setArtists(lstArtists);
}

Media::Media(const unsigned uiMediaId, const QString &sName, const unsigned uiReleaseYear, const QList<Artist> &lstArtists)
    : muiMediaId(uiMediaId)
    , mDtRelease(QDate(static_cast<int>(uiReleaseYear), 1, 1))
    , msName(sName)
{
    setArtists(lstArtists);
}

Media::Media(const unsigned uiMediaId, const QString &sName, const QDate &dtRelease, const QString &sFilePath, const QList<Artist> &lstArtists)
    : muiMediaId(uiMediaId)
    , mDtRelease(dtRelease)
    , msFilePath(sFilePath), msName(sName)
{
    setArtists(lstArtists);
}

Media::Media(const unsigned uiMediaId, const QString &sName, const unsigned uiReleaseYear, const QString &sFilePath, const QList<Artist> &lstArtists)
    : muiMediaId(uiMediaId)
    , mDtRelease(QDate(static_cast<int>(uiReleaseYear), 1, 1))
    , msFilePath(sFilePath), msName(sName)
{
    setArtists(lstArtists);
}

Media::Media(const Media &refCopy)
    : muiMediaId(refCopy.getMediaId())
    , mDtRelease(refCopy.getReleaseDate())
    , msFilePath(refCopy.getFilePath())
    , msName(refCopy.getName())
{
    setArtists(refCopy.getArtists());
}


void Media::addArtist(const Artist &refArtist)
{
    getArtistsInternal().append(refArtist);
}

const QList<Artist> Media::getArtists() const
{
    return(getArtistsInternal());
}

QString &Media::getFilePath() const
{
    return(msFilePath);
}

unsigned Media::getMediaId() const
{
    return(muiMediaId);
}

QString Media::getName() const
{
    return(msName);
}

QDate Media::getReleaseDate() const
{
    return(mDtRelease);
}

bool Media::removeArtist(const Artist &refArtist)
{
    return(getArtistsInternal().removeOne(refArtist));
}

void Media::removeArtist(const unsigned uiIndex)
{
    getArtistsInternal().removeAt(uiIndex);
}

void Media::setFilePath(const QString &sFilePath)
{
    msFilePath = sFilePath;
}

void Media::setMediaId(const unsigned uiMediaId)
{
    muiMediaId = uiMediaId;
}

void Media::setName(const QString &sName)
{
    msName = sName;
}

void Media::setReleaseDate(const QDate &dtRelease)
{
    mDtRelease = dtRelease;
}

void Media::setReleaseDate(const unsigned uiReleaseYear)
{
    mDtRelease = QDate(static_cast<int>(uiReleaseYear), 1, 1);
}

错误首先发生在CPP文件setArtists()的第二个函数中-出于任何原因,手动键入它时,它都认为lstArtist是对int的引用,但getArtists()和getArtistsInternal()仍然有效。此后,它会在以lstArtists作为参数的构造函数定义以及第一个removeArtist()(将对Artist的实际引用作为参数)上发生。

我不知道我是否在滥用QList或其他东西,因为它似乎仅在使用QList的函数上。如果需要,我可以粘贴Artist的类定义(头文件),但我认为没有必要。

这不可能是因为#pragma一旦我放在顶部,是吗? QT似乎以#ifndef和#define开始所有类文件,并以#endif结束它-但这给我造成了其他问题,其中其他类引用了父对象,而父类又有子代列表。 / p>

当我在头文件中内联这些函数(它抱怨的地方)时似乎很满意,但是它在CPP文件中不喜欢它们,并且我不#include QList在CPP文件中,因为它包含在内在头文件中。

有什么想法吗?

P.S。如果您希望以其他方式查看它,可以从https://github.com/sfulmer/MediaDB签出,文件为Media.h和Media.cpp。

1 个答案:

答案 0 :(得分:1)

我想我已经解决了我的问题-我认为#pragma一次行必须在文件的第一行-移动后所有错误都消失了。