开发者

Vector.h Header file (using Stanford C Library)is corrupted or cannot be used

开发者 https://www.devze.com 2023-01-24 19:52 出处:网络
The errors are appearing only with file streams After removing vector.h file, the program runs properly

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(f开发者_JS百科ilename); 
        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


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.


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.


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.


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)?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消