C++ 使用派生类的基类方法-错误

C++ 使用派生类的基类方法-错误,c++,string,class,inheritance,C++,String,Class,Inheritance,我正在为一篇大学论文做一个小项目,我遇到了一些麻烦 我有一个类publication,它有标题和文本字段,定义如下(这是头文件): 这是实现(.cpp文件) #pragma一次 #包括 #包括 #包括“第h条” 使用名称空间std; 使用std::string; article::article():publication() { 作者=”; } void article::set_author(常量字符串new_author) { 作者=新作者; } 字符串文章::get_author() {

我正在为一篇大学论文做一个小项目,我遇到了一些麻烦

我有一个类
publication
,它有标题和文本字段,定义如下(这是头文件):

这是实现(.cpp文件)

#pragma一次
#包括
#包括
#包括“第h条”
使用名称空间std;
使用std::string;
article::article():publication()
{
作者=”;
}
void article::set_author(常量字符串new_author)
{
作者=新作者;
}
字符串文章::get_author()
{
返回作者;
}
string article::ToString()
{
字符串返回;
ToReturn=“Author:“+Author+'\n'+article.get_headline()+'\n'+article.get_text();
回归回归;
}
为了测试一切正常,我编写了以下主要函数:

#pragma once
#include "article.h"
#include "news.h"
#include "notice.h"
#include <conio.h>

using namespace std;
using std::string;

void main()
{
    article MyArticle;

    MyArticle.set_author("Thomas H. Cormen");
    MyArticle.set_headline("Introduction to Algorithms");
    MyArticle.set_text("Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph.");

    cout << MyArticle.ToString();
    getch();
}
#pragma一次
#包括“第h条”
#包括“news.h”
#包括“notice.h”
#包括
使用名称空间std;
使用std::string;
void main()
{
第条我的文章;
我的文章集作者(“托马斯H.科曼”);
MyArticle.set_标题(“算法简介”);
set_text(“Dijkstra算法是一种寻找图中节点间最短路径的算法”);

cout
article
是一个类,它不能用于
的左侧(只有对象可以)。实际上,您根本不需要在那里进行限定:

ToReturn = "Author: " + author + '\n' + get_headline() + '\n' + get_text();
如果出于某种原因,您确实希望限定继承的成员,那么您可以使用范围解析运算符(
)来实现:

ToReturn = "Author: " + author + '\n' + article::get_headline() + '\n' + article::get_text();

但是请记住,在这里您不必这样做(并且在大多数编码约定下,您不应该这样做)。例如,如果函数是虚拟的,那么明确地限定它们甚至可能是错误的(因为这样会抑制虚拟分派)如果你已经提到他可以使用
,我想你也应该提到他可以使用
这个->
。我不熟悉任何编码惯例(但我自己不熟悉),但我认为
这个->
在某些情况下可以让代码更清晰
#pragma once
#include <iostream>
#include <string>
#include "article.h"

using namespace std;
using std::string;

article::article(): publication()
{
    author="";
}

void article::set_author(const string new_author)
{
    author=new_author;
}

string article::get_author()
{
    return author;
}

string article::ToString()
{
    string ToReturn;
    ToReturn = "Author: " + author + '\n' + article.get_headline() + '\n' + article.get_text();
    return ToReturn;
}
#pragma once
#include "article.h"
#include "news.h"
#include "notice.h"
#include <conio.h>

using namespace std;
using std::string;

void main()
{
    article MyArticle;

    MyArticle.set_author("Thomas H. Cormen");
    MyArticle.set_headline("Introduction to Algorithms");
    MyArticle.set_text("Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph.");

    cout << MyArticle.ToString();
    getch();
}
ToReturn = "Author: " + author + '\n' + get_headline() + '\n' + get_text();
ToReturn = "Author: " + author + '\n' + article::get_headline() + '\n' + article::get_text();