简体   繁体   English

C ++简单程序帮助字符串数据

[英]C++ simple program help String Data

Thanks for helping me in advance! 感谢您提前帮助我! I am used to Java and C# but C++ is completely different. 我习惯Java和C#,但C ++完全不同。 I am trying to do part B of this lab: http://cs.binghamton.edu/~sgreene/cs240-2010f/labs/lab2.html 我正在尝试做本实验的B部分: http : //cs.binghamton.edu/~sgreene/cs240-2010f/labs/lab2.html

Is this the idea that is assigned in Part B? 这是B部分中分配的想法吗? Also I dont know what exactly is missing in the header file that deals with indef endif, etc. I did research and I dont seem to miss anything. 我也不知道在处理indef endif等的头文件中到底缺少什么。我做了研究,但似乎什么也没错过。 And lastly, what does the lab assignment mean when it says: "In the [] operator, the index should be checked to confirm it is between 0 and 9. If it is not, the string "Undefined" should be returned instead." 最后,实验室分配说的是什么意思:“在[]运算符中,应检查索引以确认它在0到9之间。如果不是,则应返回字符串“ Undefined”。 Thanks 谢谢

ok so here are my updated files 好的,所以这是我的更新文件

#include "TenStrings.h"

using namespace std;

//Default Constructor
TenStrings::TenStrings()
{
    public:

    TenStrings str[10];
    str[0] = "String 1";
    str[1] = "String 2";
    str[2] = "String 3";
    str[3] = "String 4";
    str[4] = "String 5";
    str[5] = "String 6";
    str[6] = "String 7";
    str[7] = "String 8";
    str[8] = "String 9";
    str[9] = "String 10";
}
;

main.cpp --------------------------*/ main.cpp -------------------------- * /

#include "TenStrings.h"
#include <iostream>

int main()
{
    TenStrings varTen;

    std::cout << str[2] << std::endl;
    return 0; 
}

also is the line missing in my header file: #include ? 我的头文件中也缺少该行:#include吗?

I notice that the assignment carries this warning: 我注意到该作业带有以下警告:

This is an individual assignment - you may NOT work in groups. 这是一项个人任务-您可能无法分组工作。 Do not write or dictate code for anyone else, and do not use code from anyone else. 不要为他人编写或指定代码,也不要使用他人的代码。 If you break this rule, you may fail the entire class. 如果违反此规则,则可能会使整个班级不及格。

Hence I will provide only some guidance, not code. 因此,我将只提供一些指导,而不是代码。

First I dont know where to create the new data strings, whether it should be in my header class or tenstrings.cpp? 首先,我不知道在哪里创建新的数据字符串,无论它应该在我的标头类还是tenstrings.cpp中?

The assignment says: 作业说:

Implement this TenStrings class such that it acts as an array of 10 constant-data strings. 实现此TenStrings类,使其充当10个常量数据字符串的数组。 Start with the basic [] operator, and the constructor. 从基本的[]运算符和构造函数开始。

The word "implement" is key. “实施”一词是关键。 Until you know about inline function definitions (which I'm guessing you haven't gotten to yet), implementation always needs to be in one of the cpp files (in this case, tenstrings.cpp .) 直到您了解内联函数定义 (我猜您还没有了解)之前,实现始终都必须位于一个cpp文件中(在本例中为tenstrings.cpp

Should I create an array of empty strings? 我应该创建一个空字符串数组吗? or an array of String1, string2, string3, etc? 还是String1,string2,string3等的数组?

The assignment says: 作业说:

In the constructor, the 10 strings should initially be set to "First String", "Second String", etc.... 在构造函数中,最初应将10个字符串设置为“ First String”,“ Second String”等。

In other words, the constructor for the TenString class should make the first string (ie, at index [0] ) have the exact value "First String" ; 换句话说,TenString类的构造函数应使第一个字符串(即,在索引[0] )具有确切的"First String" and so on up through the tenth string (at index [9] ) having the exact value "Tenth String" . 依此类推,直到第十个字符串(在索引[9] )都具有精确"Tenth String" Apparently, the intent is to be able to change these assignments through the [] operator, which you are to write as part of the TenString class. 显然,其目的是能够通过[]运算符更改这些分配,您将把它们写为TenString类的一部分。

I hope this helps. 我希望这有帮助。 (I think that I've interpreted the assignment correctly, but I can't guarantee that.) (我认为我已经正确地解释了作业,但是我不能保证。)

Oh, and by the way, there's no need for this line: 哦,顺便说一句,不需要此行:

#include <string>

since the assignment specifically says not to use the String class. 因为该分配明确指出不要使用String类。

EDIT : Also, the header file seems to be missing a line at the beginning. 编辑 :另外,头文件似乎在开头缺少一行。 The missing line is related to the #ifndef / #define / #endif protection that's mentioned in the assignment. 缺少的行与作业中提到的#ifndef / #define / #endif保护有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM