繁体   English   中英

编译两个程序失败

[英]Compiling two programs fails

对于一项作业,我编写了两个程序。 一个用于生成随机整数文件,另一个用于计算小于指定阈值的整数。 您可以在我发布的代码下方找到实际的作业文本。

编译 g++ generate.cpp -o generate 时,出现此错误:

    z1755294@hopper:~/assign2$ g++ generate.cpp -o generate
generate.cpp: In function ‘bool writeRand(int, int, int, const char*)’:
generate.cpp:12:31: error: variable ‘std::ofstream fout’ has initializer but incomplete type
         ofstream fout ( fname );

当我编译 g++ thresh.cpp -o thresh 时,出现此错误:

    z1755294@hopper:~/assign2$ g++ thresh.cpp -o thresh
thresh.cpp: In function ‘int main()’:
thresh.cpp:19:16: error: variable ‘std::ifstream fin’ has initializer but incomplete type
  ifstream fin( fname.c_str() );

任何人都可以帮助我修复我的代码以使其正常工作吗? 由于我有多个可执行文件,我还需要为我的项目创建一个 Makefile 吗?

非常感谢......有点坚持要做什么。


这是我的代码:

生成.cpp

#include <iostream>
#include <cstdlib>  // re. atoi

using std::cout;
using std::endl;
using std::cerr;
using std::ifstream;
using std::ofstream;

bool writeRand ( const int ranSeed, const int maxVal, const int numVals, const char* fname )
{
        ofstream fout ( fname );
        if ( fout )
        {
                srand ( ranSeed );
                for ( int i=0; i < numVals; ++ i )
                        fout << rand () % (maxVal+1) << endl;
                fout.close ();
                return true;
        }
        //else
        return false;
}

int main ( int argc, char* argv [] )
{
        if (argc !=5 )
        {
                cerr << "Usage: " << argv[0] << "ranSeed maxVal numVals outFileName" << endl;
                return -1;
        }

        const int ranSeed = atoi(argv[1]);
        const int maxVal = atoi(argv[2]);
        const int numVals = atoi(argv[3]);
        const char* fname = argv[4];

        if ( ranSeed <= 0 || maxVal <= 0 || numVals <= 0 )
        {
                cerr << "Invalid negative or zero numbers on command line....Try again..." << endl;
                return -1;
        }

        if ( writeRand( ranSeed, maxVal, numVals, fname ) )
                cout << "ranSeed = " << ranSeed << ", maxVal = " << maxVal << ", numVals = " << numVals
                << "\nfame " << fname << " was created ok ..." << endl;
        else
                cout << "There was a problem creating file " << fname << " ..." << endl;
}

阈值文件

#include <iostream>
#include <cstdlib> // re. atoi

using std::cout;
using std::endl;
using std::cin;
using std::ifstream;
using std::string;
using std::flush;

int main ()
{
        //prompt and take in the desired file name
        cout << "Enter name of file (for example randNums.txt): " << flush;
        string fname;
        getline( cin, fname );

        //then can open file
        ifstream fin( fname.c_str() );
        if( fin )
        {
                int max, count = 0, below = 0, val = 0;
                string line;
                while( true )
                {
                        cout << "Enter the threshold value: " << flush;
                        getline( cin,line );
                        max = atoi( line.c_str() );

                        if( max > 0 ) break;
                        cout << "Try again with value > 0 \n";
                }

                while( getline( fin, line) )
                {
                        val = atoi( line.c_str() );
                        ++count;

                        if( val < max ) ++below;
                }
                fin.close();

                cout << below << " of " << count << " values in file '"
                     << fname << "' are less than " << max << '\n';

                max = val+1; //last value (in file) + 1
                count = 0, below = 0;
                fin.open( fname.c_str() );
                while( getline( fin, line ) )
                {
                        int val = atoi( line.c_str() );
                        ++count;

                        if( val < max ) ++below;
                }

                fin.close();

                cout << below << " of " << count << " values in file '"
                     << fname << "' are less than " << max << '\n';

        }
        else
 cout << "There was an error opening file " << fname << '\n';


        cout << "Please 'Enter' to continue/exit..." << flush;
        cin.get();
        return 0;
}

作业

产生

创建一个名为“generate”的程序,它生成一个随机整数文件。 该程序采用四个命令行参数。 他们是,按顺序

*一个随机数种子。 这是下面解释的整数值。 *最大值。 随机值应小于此最大值。 该值是一个整数。 *要生成的值的数量 *存储值的输出文件的名称

如果没有给出命令行参数,程序应该给出一个关于正确使用的简短信息,然后退出。 命令行上的整数值都应该检查负值。 如果给出负值,程序应打印错误消息并退出。 随机数生成

rand() 函数在每次调用时都会返回一个随机正整数。 srand(int) 函数采用一个称为随机数种子的整数,用于初始化随机数生成器。 对 rand() 的后续调用会产生一个随机序列,该序列与其种子唯一相关。 通常在程序中, srand() 被调用一次,而 rand() 被调用多次。

生成特定大小的随机数的常用技术是使用 rand() 除以最大上限的余数。

脱粒

创建一个名为 thresh 的程序,它会询问用户一个文件名和一个整数阈值。 然后程序应该计算文件中小于阈值的值的数量并报告结果。

例如,mydatafile 中 300 个值中有 43 个小于 17 该程序不应使用命令行参数。 相反,应该通过提示用户来获得所需的值。

当请求阈值时,如果用户输入一个负值,程序应该打印一个错误并循环请求直到获得一个合适的值。

如果输入文件不存在,则应打印错误消息并退出程序。

提示:

使用可以手动检查的小值来测试您的程序。 例如,创建一个包含 20 个小于 10 的值的文件。 thresh 的一个特别好的测试是使用数据文件的最后一个值作为阈值。 然后使用最后一个值加一作为阈值。

**Makefile 是您提交的一部分。 如果项目有多个可执行文件,您应该有一个默认的 makefile 规则来构建所有的可执行文件。 你应该有一个规则来清除项目到原始状态。 你的 Makefile 应该使用适当的变量。

正如@mrunion 指出的,你应该更换

g++ generate.cpp thresh.cpp

使用g++ generate.cpp

g++ thresh.cpp

顺便说一句,如果您背靠背执行这些操作,则会覆盖您的可执行文件。 一个改进是:

g++ generate.cpp -o generate

g++ thresh.cpp -o thresh

暂无
暂无

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

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