2012-07-09 11 views
1

林真的很郁闷,因此我真的可以使用一些帮助。需要帮助创建一个简单的控制台程序,使用头文件我写

我在visual studio中创建了一个新项目。我首先创建了一个名为“MyString,h”的新头文件,并将其放在头文件夹中。它包含一个名为String的类。你可以在这个

的末尾看到我使用的代码。我现在也在源文件文件夹中有一个MyStringTest.cpp文件。它有以下代码。

#include <iostream> 
    #include "MyString.h" 
    using namespace std; 

    int main() { 
      String obj = "Hello"; 
     cout << obj(1,3); 
    } 

预计:编译和运行控制台程序将输出“LLO” 现实:错误:“标识‘字符串’是不确定的

下面是我的一些在头文件中的代码...我真的不适合所有这一切。

//1. Preprocessor commands - guards against multiple inclusions of the file MyString.h 
    #ifdef __MYSTRING_H__ 
    #define __MYSTRING_H_ 
    #define _CRT_SECURE_NO_DEPRECATE 
    #define _CRT_NONSTDC_NO_DEPRECATE 

    //2. Include Files for String Methods and Assert 
    #include<cstring> //strlen, strcpy, strcmp 
    #include<cassert> //assert 
    #include<iostream> //cout, cin 
    using namespace std; 

    //3. Begin the String Class Interface 
    class String{ 

    //4. Define the Public Members 
    public: 

     //5. Default Constructor 
     String(); 

     //6. Constructor which converts a char* to a String object 
     String(const char *s); 

....

+1

标识符出发有两个下划线留作执行。我使用'MYSTRING_H',你会看到诸如'MYSTRING_H_INCLUDED'之类的东西。标识符以一个下划线开头,后面跟一个大写字母(这两个都至少适用于IIRC的任何范围),但从它的外观来看,'_CRT *'的东西不是你的,无论如何只要知道就好。 – chris 2012-07-09 23:51:30

回答

2

在第一行代码有一点差错:你需要

#ifndef __MYSTRING_H__ 

否则你排除您所有的文件内容直到#endif

+0

哦,我爱你。 :) 非常感谢! – egucciar 2012-07-09 23:12:30

相关问题