1

所以这就是我目前拥有的,我的教授提供的扫描仪。

#include "Similarity.h"
#include "Scanner.h"
using namespace std;

int Similarity::readData(Scanner inFile){
    int similarityInputSize;
    vector< vector<int> > containingVector;
    bool nextValueFound;
    similarityInputSize = inFile.nextInt();
    int lineCount = 0;
    while(inFile.hasNext()){
            containingVector.push_back(vector<int>());
            for(int i = 0; i < similarityInputSize; i++){
                    containingVector[lineCount].push_back(inFile.nextInt());
            }
            lineCount++;
    }
    for(int i = 0; i < containingVector.size(); i++){
            for(int j = 0; j < similarityInputSize; i++){
                    cout << containingVector[i][j] << " ";
            }
            cout << endl;
    }
    return 0;
 }

主要课程是给我们的,涉及价值调用而不是引用,我的教授开始工作。

我收到的错误是:

 In file included from /usr/include/c++/4.7/ios:43:0,
             from /usr/include/c++/4.7/ostream:40,
             from /usr/include/c++/4.7/iostream:40,
             from ../../Utilities/Utils.h:17,
             from Main.h:11:
 /usr/include/c++/4.7/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
 /usr/include/c++/4.7/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
 In file included from /usr/include/c++/4.7/ios:45:0,
             from /usr/include/c++/4.7/ostream:40,
             from /usr/include/c++/4.7/iostream:40,
             from ../../Utilities/Utils.h:17,
             from Main.h:11:
 /usr/include/c++/4.7/bits/basic_ios.h:64:11: error: within this context
 In file included from ../../Utilities/Utils.h:18:0,
             from Main.h:11:
 /usr/include/c++/4.7/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
 /usr/include/c++/4.7/fstream:420:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here 
 In file included from /usr/include/c++/4.7/ios:44:0,
             from /usr/include/c++/4.7/ostream:40,
             from /usr/include/c++/4.7/iostream:40,
             from ../../Utilities/Utils.h:17,
             from Main.h:11:
  /usr/include/c++/4.7/streambuf: In copy constructor ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’:
  /usr/include/c++/4.7/streambuf:800:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const __streambuf_type&) [with _CharT = char; _Traits =      std::char_traits<char>; std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private
  In file included from ../../Utilities/Utils.h:18:0,
             from Main.h:11:
  /usr/include/c++/4.7/fstream:69:11: error: within this context
  /usr/include/c++/4.7/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
  /usr/include/c++/4.7/fstream:420:11: note: synthesized method ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ first required here 
In file included from Main.h:12:0:
../../Utilities/Scanner.h: In copy constructor ‘Scanner::Scanner(const Scanner&)’:
../../Utilities/Scanner.h:27:7: note: synthesized method ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’ first required here 
Main.cpp: In function ‘int main(int, char**)’:
Main.cpp:58:31: note: synthesized method ‘Scanner::Scanner(const Scanner&)’ first required here 
In file included from Main.h:14:0:
Similarity.h:23:9: error:   initializing argument 1 of ‘int Similarity::readData(Scanner)’

所以我不太明白发生了什么。我到处都说这个问题将通过引用而不是值调用来解决。但是,他提供的代码不包含任何引用调用,这让我认为这是一个更微妙的错误(或者它太大以至于让我眼花缭乱)。尽管如此,我还是改变了它,它给了我这些错误:

Similarity.cpp:12:5: error: prototype for ‘int Similarity::readData(Scanner&)’ does not match any in class ‘Similarity’
Similarity.h:23:9: error: candidate is: int Similarity::readData(Scanner)
Main.cpp: In function ‘int main(int, char**)’:
Main.cpp:58:32: error: no matching function for call to ‘Similarity::readData(Scanner*)’
Main.cpp:58:32: note: candidate is:
In file included from Main.h:14:0:
Similarity.h:23:9: note: int Similarity::readData(Scanner)
Similarity.h:23:9: note:   no known conversion for argument 1 from ‘Scanner*’ to ‘Scanner’

你能告诉我的任何事情都会有帮助。

提前致谢!

4

1 回答 1

1

我猜 Scanner 并不是要被复制的。语法:

int Similarity::readData(Scanner inFile){

意味着 Scanner 将通过 Scanner 的复制构造函数从传入的变量复制到此方法接收的变量。

Scanner 听起来像是在后台处理 iostream 的东西。iostreams 库中的流没有复制构造函数

我不完全清楚您如何尝试通过引用传递,但您需要将此文件和相应的 .h 文件中的签名更改为:

int Similarity::readData(Scanner& inFile){

你应该没有问题。readData现在收到对传入文件的引用。这本质上是传入的东西的别名——你对它所做的任何事情都会反映在传入的对象上。

于 2013-09-19T02:40:18.497 回答