Vector.h 头文件(使用斯坦福 C 库)已损坏或无法使用

发布于 2024-10-02 01:47:41 字数 3598 浏览 1 评论 0原文

这些错误仅出现在文件流中
删除vector.h文件后,程序正常运行
我想在未来的程序中使用vector.h

    // Using Stanford CS106B Library    

        #include "genlib.h"
        #include "vector.h"  // After removing this file, the program runs but i cannot use vector
        #include <fstream>
        #include <iostream>
        #include <simpio.h>

        const int ALPHABET_SIZE=26;

    void CountLetters(string filename){

        ifstream in;  // line 22
        in.open(filename.c_str());  // line 23  
        if(in.fail()) Error("Error opening file or File does not exist"); // line 24

        int letters[ALPHABET_SIZE];
        string line;

        for(int i=0;i<ALPHABET_SIZE;i++)
            letters[i]=0;



        while(true){
            getline(in,line); // line 35
            if(in.fail()) break;  // line 36
            for(int i=0;i<line.length();i++){
                char c= line.at(i);
                if( c >= 'a' && c <= 'z')
                    letters[c-'a']++;
                if( c >= 'A' && c <= 'Z')
                    letters[c-'A']++;
            }
        }

        for(int i=0;i<ALPHABET_SIZE;i++)
            cout<<char('A'+i)<<" "<<letters[i]<<endl;
    }


    int main(){
            cout << "Enter the filename: ";
            string filename = GetLine();
            CountLetters(filename); 
        return 0;
    }

CompileC build/section1.build/Debug/assn-0-narcissism.build/Objects-normal/i386/vectorcountletters.o vectorcountletters.cpp 普通i386 c++ com.apple.compilers.gcc。 4_2 cd“/Users/rishi/Downloads/ms-cs/cs106b/code/section1” setenv LANG en_US.US-ASCII /Developer/usr/bin/gcc-4.2 -x c++ -arch i386 -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot /Developer /SDKs/MacOSX10.5.sdk -mfix-and-continue -fvisibility-inlines-hidden -mmacosx-version-min=10.5 -gdwarf-2“-I/Users/rishi/Downloads/ms-cs/cs106b/code/ section1/build/section1.build/Debug/assn-0-narcissism.build/blank-project.hmap”-Wall -Wno-sign-compare -F。 -ics106 -Iinclude“-I/Users/rishi/Downloads/ms-cs/cs106b/code/section1/build/section1.build/Debug/assn-0-narcissism.build/DerivedSources/i386”“-I/Users/ rishi/Downloads/ms-cs/cs106b/code/section1/build/section1.build/Debug/assn-0-narcissism.build/DerivedSources" -fstack-check -c"/Users/rishi/Downloads/ms-cs/ cs106b/code/section1/vectorcountletters.cpp" -o "/Users/rishi/Downloads/ms-cs/cs106b/code/section1/build/section1.build/Debug/assn-0-narcissism.build/Objects-normal/ i386/vectorcountletters.o"

/Users/rishi/Downloads/ms-cs/cs106b/code/section1/vectorcountletters.cpp:在函数“void CountLetters(std::string)”中: /Users/rishi/Downloads/ms-cs/cs106b/code/section1/vectorcountletters.cpp:22:错误:“=”标记之前预期有不合格 ID /Users/rishi/Downloads/ms-cs/cs106b/code/section1/vectorcountletters.cpp:23:错误:“=”标记之前的预期主表达式 /Users/rishi/Downloads/ms-cs/cs106b/code/section1/vectorcountletters.cpp:23:错误:“.”之前预期的主表达式代币 /Users/rishi/Downloads/ms-cs/cs106b/code/section1/vectorcountletters.cpp:24:错误:“=”标记之前的预期主表达式 /Users/rishi/Downloads/ms-cs/cs106b/code/section1/vectorcountletters.cpp:24:错误:“.”之前预期的主表达式代币 /Users/rishi/Downloads/ms-cs/cs106b/code/section1/vectorcountletters.cpp:35:错误:“=”标记之前的预期主表达式 /Users/rishi/Downloads/ms-cs/cs106b/code/section1/vectorcountletters.cpp:35:错误:“,”标记之前的预期主表达式 /Users/rishi/Downloads/ms-cs/cs106b/code/section1/vectorcountletters.cpp:36:错误:“=”标记之前的预期主表达式 /Users/rishi/Downloads/ms-cs/cs106b/code/section1/vectorcountletters.cpp:36:错误:“.”之前预期的主要表达式代币

The errors are appearing only with file streams
After removing vector.h file, the program runs properly
I want to use vector.h in future programs

    // Using Stanford CS106B Library    

        #include "genlib.h"
        #include "vector.h"  // After removing this file, the program runs but i cannot use vector
        #include <fstream>
        #include <iostream>
        #include <simpio.h>

        const int ALPHABET_SIZE=26;

    void CountLetters(string filename){

        ifstream in;  // line 22
        in.open(filename.c_str());  // line 23  
        if(in.fail()) Error("Error opening file or File does not exist"); // line 24

        int letters[ALPHABET_SIZE];
        string line;

        for(int i=0;i<ALPHABET_SIZE;i++)
            letters[i]=0;



        while(true){
            getline(in,line); // line 35
            if(in.fail()) break;  // line 36
            for(int i=0;i<line.length();i++){
                char c= line.at(i);
                if( c >= 'a' && c <= 'z')
                    letters[c-'a']++;
                if( c >= 'A' && c <= 'Z')
                    letters[c-'A']++;
            }
        }

        for(int i=0;i<ALPHABET_SIZE;i++)
            cout<<char('A'+i)<<" "<<letters[i]<<endl;
    }


    int main(){
            cout << "Enter the filename: ";
            string filename = GetLine();
            CountLetters(filename); 
        return 0;
    }

CompileC build/section1.build/Debug/assn-0-narcissism.build/Objects-normal/i386/vectorcountletters.o vectorcountletters.cpp normal i386 c++ com.apple.compilers.gcc.4_2
cd "/Users/rishi/Downloads/ms- cs/cs106b/code/section1"
setenv LANG en_US.US-ASCII
/Developer/usr/bin/gcc-4.2 -x c++ -arch i386 -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot /Developer/SDKs/MacOSX10.5.sdk -mfix-and-continue -fvisibility-inlines-hidden -mmacosx-version-min=10.5 -gdwarf-2 "-I/Users/rishi/Downloads/ms- cs/cs106b/code/section1/build/section1.build/Debug/assn-0-narcissism.build/blank-project.hmap" -Wall -Wno-sign-compare -F. -Ics106 -Iinclude "-I/Users/rishi/Downloads/ms- cs/cs106b/code/section1/build/section1.build/Debug/assn-0-narcissism.build/DerivedSources/i386" "-I/Users/rishi/Downloads/ms- cs/cs106b/code/section1/build/section1.build/Debug/assn-0-narcissism.build/DerivedSources" -fstack-check -c "/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp" -o "/Users/rishi/Downloads/ms- cs/cs106b/code/section1/build/section1.build/Debug/assn-0-narcissism.build/Objects-normal/i386/vectorcountletters.o"

/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp: In function 'void CountLetters(std::string)':
/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp:22: error: expected unqualified-id before '=' token
/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp:23: error: expected primary-expression before '=' token
/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp:23: error: expected primary-expression before '.' token
/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp:24: error: expected primary-expression before '=' token
/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp:24: error: expected primary-expression before '.' token
/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp:35: error: expected primary-expression before '=' token
/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp:35: error: expected primary-expression before ',' token
/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp:36: error: expected primary-expression before '=' token
/Users/rishi/Downloads/ms- cs/cs106b/code/section1/vectorcountletters.cpp:36: error: expected primary-expression before '.' token

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

耳根太软 2024-10-09 01:47:41

现在回答这个问题可能已经太晚了,但我现在正在浏览斯坦福大学的CS106B课程材料,我遇到了同样的问题。我找到了解决这个问题的简单解决方案,所以我认为值得一提,以防有人偶然发现同样的问题。

我使用 Linux 版本的起始代码来完成作业。在这种情况下,您将获得一个 Makefile 以及起始代码,它生成斯坦福 C++ 库的静态库,您可以在代码中使用它。 makefile 工作正常。你必须打开终端,进入目录,比如0-warmup(第一个作业的目录),其中包含Makefile、warmup.cpp、spl.jar和StanfordCPPLib目录。 Warmup.cpp 有两个错误(修复是作业),修复这两个错误。然后您可以在终端中键入“make”来构建可执行文件并运行它。它会工作得很好。

当您在代码中包含 vector.h 标头(其中 vector.h 是在 StanleyCPPLib 目录中定义)时,就会出现此问题。现在,您的 cpp 文件将在编译时生成错误,表示无法找到“vector.h”文件,即使它可以找到也在斯坦福 c++ 库中定义的“simpio.h”。不仅是“vector.h”会出现这种情况,在StanfordCPPLib目录下没有对应cpp文件的其他头文件也会出现这种情况,例如foreach.h、graph.h、grid.h等。

解决方法非常简单。不要仅包含“vector.h”,包含 vector.h 文件的相对路径。由于它位于当前目录的StanfordCPPLib目录中,因此在cpp文件中包含“StanfordCPPLib/vector.h”(#include“StanfordCPPLib/vector.h”)。现在输入“make”来执行Makefile,它应该生成可执行文件,没有任何错误。

It's probably too late to answer the question, but I'm going through the CS106B course materials of Stanford University right now and I faced the same problem. I found a simple solution for this problem, so I thought its worth mentioning in case someone stumbles on the same problem.

I am using the linux version of the starter code for the assignments. In that case, you are given a Makefile along with the starter code, that generates a static library of the Stanford C++ library and you can use it in your code. The makefile works fine. You have to open the terminal, go the the directory, say 0-warmup (the directory of the very first assignment), which contains Makefile, warmup.cpp, spl.jar and StanfordCPPLib directory. The warmup.cpp has two errors (fixing is the assignment), fix these two errors. Then you can type "make" in the terminal to build the executables and run it. It will work fine.

The problem occurs when you include vector.h header in your code where vector.h in defined in the StanfordCPPLib directory. Now Your cpp file will generate an error at compile time, saying that it cannot find the "vector.h" file, even though it can find "simpio.h" which is also defined in the Stanford c++ library. It happens not only with "vector.h" but also with other header files for which there is no corresponding cpp file in StanfordCPPLib directory, such as foreach.h, graph.h, grid.h, etc.

The solution is very simple. Do not include just "vector.h", include the relative path to vector.h file. Since it resides in the StanfordCPPLib directory of current directory, include "StanfordCPPLib/vector.h" (#include "StanfordCPPLib/vector.h") in the cpp file. Now type "make" to execute the Makefile, and it should generate the executable file without any error.

回忆那么伤 2024-10-09 01:47:41

我今天遇到了同样的问题,并发现在使用斯坦福大学的 vector.h 库时,“in”ifstream 类型变量不能被命名为“in”。我没有花时间进一步研究这一点,但看起来库中的某个地方另一个变量也必须命名为“in”,并且编译器混淆了两者。因此,将 ifstream 变量的名称更改为“in”以外的名称,这样就可以解决错误。

I ran into this same problem today and figured out that the "in" ifstream type variable cannot be named "in" while using Stanford's vector.h library. I didn't take the time to look into this further, but it looks like somewhere in the library another variable must also be named "in" and the compiler is confusing the two. So, change the name of your ifstream variable to something besides "in" and this takes care of the error.

世界和平 2024-10-09 01:47:41

我似乎无法获得向量的完整源代码,我只看到 Doxygen,但除非 Doxygens 去掉 const,否则该向量类的所有方法似乎都不是 const,而且我真的认为任何人都没有理由重写部分内容的标准库,我不明白该类有什么好处。

I can't seem to get the whole source of vector, I see only the Doxygen but unless Doxygens strips out consts, it looks like none of the methods of that vector class are const, and I really see no reason for anyone to rewrite parts of the standard library, I don't see what the benefit is at all of that class.

身边 2024-10-09 01:47:41

错误报告第 22 行中有一个“=”,以及“.”。和 '=' 分别位于第 23 行和第 24 行。

列出的代码中没有出现这种情况。

更糟糕的是:第 35 行的错误报告了一个“,”(逗号)。你的程序中有两个逗号:一个在顶部的注释中,另一个在 getline(in,line); 的中间。也令人困惑。

所以你的错误与你的代码不匹配!

我能做出的最好的猜测是,你的代码中的某些内容已经被 #define 为完全不同的内容。难道某个标头中的某处有一行 #define in (some bad) 吗?

The errors report that there is a '=' in line 22, and '.' and '=' each in lines 23 and 24.

The is no part of the listed code where this happens.

Worse: the error about line 35 reports a ',' (comma). There are two commas in your program: one in the comment at the top, the other in getline(in,line); halfway. Confusing as well.

So your errors do not match your code!

The best guess I can make, is that something in your code has been #defined to something completely else. Could it be that somewhere in one of the headers is a line #define in (some mess)?

懵少女 2024-10-09 01:47:41

尽管听起来很愚蠢,但尝试在 vector.h 的 #include 之后添加一个空行

另外 - 查看 vector.h 的文档 - 这意味着您需要在源代码中添加一些额外的包含文件。

尝试
#include“genlib.h”
#include“strutils.h”
#include cstdlib

Silly as it may sound, try putting a blank line after the #include for vector.h

Also - look at the doccers for vector.h - the implication is that you need a few extra include files in your source.

Try
#include "genlib.h"
#include "strutils.h"
#include cstdlib

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文