C++调试中的一些小问题—[Error] within this context

YCMyTot 2014-04-17 04:10:30
类的定义:
#include<string>
#include<iostream>
using namespace std;
class Student
{
private:
string Name;//学生的姓名
public:

void setName();//接收某一学生的姓名
};
void Student::setName()
{

string name;
cout<<"请输入该学生的姓名:"<<endl;//接收某一学生的姓名

getline(cin,name);

Name=name;

}

在接受了一个字符串后将其写到一个文本文件中:
int mian()
{
。。。。。。

ofstream studentoutfile("studentoutput.txt",ios::out); //写文件开始
Student *pt1;//定义一个student类的动态对象指针
pt1=new Student;
pt1->setName();//调用Student类的成员函数
studentoutfile<<"学生的姓名为: "<<pt1->Name<<endl;//写入某一学生的姓名
、、、、、、、、
}

额。。有点复杂,,请大神帮一下哈!!
...全文
6533 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
YCMyTot 2014-04-17
  • 打赏
  • 举报
回复
在定义了 getName();函数后 void Student::getName() { int i; for(i=0;Name[i]!='\0';i++)//返回学生的姓名 { cout<<Name[i]; } } 在main函数体内 : ofstream studentoutfile("studentoutput.txt",ios::out); //写文件开始 Student *pt1;//定义一个student类的动态对象指针 pt1=new Student; pt1->setName();//调用Student类的成员函数 studentoutfile<<"学生的姓名为: "<<pt1->getName()<<endl;//写入某一学生的姓名 报错为: [Error] ")) << pt1->Student::getName()' ??????!!!
YCMyTot 2014-04-17
  • 打赏
  • 举报
回复
恩 懂了 使用 getName(); 应该可以吧?
NexTor 2014-04-17
  • 打赏
  • 举报
回复
但是Name是私有的成员,不是public的成员,所以不能直接访问
引用 4 楼 u014693181 的回复:
可是对于类的动态对象的建立与释放来说的话,对类的操作不就是 pt->Name 吗?
YCMyTot 2014-04-17
  • 打赏
  • 举报
回复
可是对于类的动态对象的建立与释放来说的话,对类的操作不就是 pt->Name 吗?
NexTor 2014-04-17
  • 打赏
  • 举报
回复
studentoutfile<<"学生的姓名为: "<<pt1->Name<<endl;//写入某一学生的姓名 Name是private的,肯定不能在main函数中直接pt1->Name这么用呀
ztenv 版主 2014-04-17
  • 打赏
  • 举报
回复
studentoutfile<<"学生的姓名为: "<<pt1->Name<<endl;//写入某一学生的姓名 不能使用pt1->Name;加一个getName()的方法吧
YCMyTot 2014-04-17
  • 打赏
  • 举报
回复
错误为: Student.h [Error] 'std::string Student::Name' is private 和 [Error] within this context ???求助!!
Visual Assist X Build 1940 requires software maintenance through 2013.05.30 (General release.) New! Context-insensitive Highlight Active Word feature for unparsed files (such as text files) as well as strings and comments in parsed files. Enabled via Options | Advanced | Refactoring | Automatically highlight words matching comment under cursor. (case=73003) New! Users can disable Surround With for individual characters '/*{#( via the registry. (case=73487) 11315 New! Changed default C++ Create From Usage snippet to use std::logic_error instead of std::exception. (case=72946) 11314, 11282 Fixed crashes identified via Windows Error Reporting. (case=73742, case=73743, case=73744, case=73745, case=74097) Improved load time for solutions with tool-generated projects that are regenerated without change to contents. (case=74014) Case-sensitive filtering for Find Symbol in Solution and Open File in Solution introduced in build 1936 is now optional and controlled via context menu in results list. (case=74214) 11359, 11347 Several fixes for refactoring and navigation for partially qualified symbols brought into scope with the C++ using namespace directive. (case=5315, case=7148, case=20644) 6573, 5959, 5219 Fix for Create Declaration generating C++ code with redundant scope qualifier when target class is in a namespace. (case=5277, case=6593) 10819, 10053, 9172, 8062, 7385, 6934, 6257, 5959 Fix for Create/Move Implementation stripping necessary scope qualifier from method pointer parameter. (case=73856) Fixed Goto from VA Outline when duplicate code windows open. (case=61729) 11350, 10375 Fix for Goto missing some interface implementations. (case=74344) Fix for Create Implementation generating code with extraneous whitespace under some circumstances. (case=73112) 11291 Improved support for filename matching when names contain multiple dots. (case=73757) 11333 Fixed focus issue with the suggestion list that appears following "in" in a C# foreach statement. (case=73981) 10226 Surround With feature is no longer invokable within comments, string literals, and tags in XML/HTML/XAML. (case=74045) Fix for Visual Studio reporting an exception when scrolling quickly through HTML and ASP files containing JavaScript when highlighted find results are present. (case=74466) Fix for Add Include placing the #include directive within a function block under some conditions. (case=74288) 11363 Add Include is no longer offered for std::string if #include is already present in the file. (case=74343) 11363 VS2008: Fixed intermittent crash at exit after a debugging session. (case=74338) 11368 VS2010+: Fixed highlighting issue for JavaScript code inside HTML and ASP files. (case=74469)

65,199

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧