架构x86_64的未定义符号:“std :: __ 1 :: locale :: use_facet(std :: __ 1 :: locale :: id&)const”

问题描述 投票:0回答:1

我正在为我的数据结构类开发一个项目。我从来没有使用过c ++,我几乎没有学习如何将文件单独编译成.o文件,然后将它们链接在一起制作可执行文件,所以即使不让错误给我,也很难。

我们得到一个:

  • 头文件,stats.h
  • 一个用来测试程序的文件,stats.cpp
  • 一个文件,看看我们提交的等级是多少。

我们应该创建一个:

  • 实现文件,stats.cpp,它创建一个构造函数和几个函数

Heres stats.h:

// FILE: stats.h
// CLASS PROVIDED: statistician
//   (a class to keep track of statistics on a sequence of real numbers)
//   This class is part of the namespace CISP430_A1.
//
// CONSTRUCTOR for the statistician class:
//   statistician( );the
//     Postcondition: The object has been initialized, and is ready to accept
//     a sequence of numbers. Various statistics will be calculated about the
//     sequence.
//
// PUBLIC MODIFICATION member functions for the statistician class:
//   void next(double r)
//     The number r has been given to the statistician as the next number in
//     its sequence of numbers.
//   void reset( );
//     Postcondition: The statistician has been cleared, as if no numbers had
//     yet been given to it.
//   
// PUBLIC CONSTANT member functions for the statistician class:
//   int length( ) const
//     Postcondition: The return value is the length of the sequence that has
//     been given to the statistician (i.e., the number of times that the
//     next(r) function has been activated).
//   double sum( ) const
//     Postcondition: The return value is the sum of all the numbers in the
//     statistician's sequence.
//   double mean( ) const
//     Precondition: length( ) > 0
//     Postcondition: The return value is the arithmetic mean (i.e., the
//     average of all the numbers in the statistician's sequence).
//   double minimum( ) const
//     Precondition: length( ) > 0
//     Postcondition: The return value is the tiniest number in the
//     statistician's sequence.
//   double maximum( ) const
//     Precondition: length( ) > 0
//     Postcondition: The return value is the largest number in the
//     statistician's sequence.
//
// NON-MEMBER functions for the statistician class:
//   statistician operator +(const statistician& s1, const statistician& s2)
//     Postcondition: The statistician that is returned contains all the
//     numbers of the sequences of s1 and s2.
//   statistician operator *(double scale, const statistician& s)
//     Postcondition: The statistician that is returned contains the same
//     numbers that s does, but each number has been multiplied by the
//     scale number.
//   bool operator ==(const statistician& s1, const statistician& s2)
//     Postcondition: The return value is true if s1 and s2 have the zero
//     length. Also, if the length is greater than zero, then s1 and s2 must
//     have the same length, the same  mean, the same minimum, 
//     the same maximum, and the same sum.
//     
// VALUE SEMANTICS for the statistician class:
// Assignments and the copy constructor may be used with statistician objects.

#ifndef STATS_H     // Prevent duplicate definition
#define STATS_H
#include <iostream>

namespace CISP430_A1
{
    class statistician
    {
    public:
        // CONSTRUCTOR
        statistician( );
        // MODIFICATION MEMBER FUNCTIONS
        void next(double r);
        void reset( );
        // CONSTANT MEMBER FUNCTIONS
        int length( ) const { return count; }
        double sum( ) const { return total; }
        double mean( ) const;
        double minimum( ) const;
        double maximum( ) const;
        // FRIEND FUNCTIONS
        friend statistician operator +
        (const statistician& s1, const statistician& s2);
        friend statistician operator *
        (double scale, const statistician& s);
 private:
    int count;       // How many numbers in the sequence
    double total;    // The sum of all the numbers in the sequence
    double tiniest;  // The smallest number in the sequence
    double largest;  // The largest number in the sequence
};

// NON-MEMBER functions for the statistician class
bool operator ==(const statistician& s1, const statistician& s2);
}

#endif

这是我正在处理的stats.cpp文件,到目前为止我只创建了一个构造函数并编译:

#include "stats.h"

namespace CISP430_A1
{
    statistician::statistician() : count(0), total(0) 
    {
    }

    }

这是stattest.cpp(我将它保存在HelloWorldII.cpp下)文件,我们给它测试它。我将它限制为仅实例化三个统计对象并打印出一行的部分:

// FILE: stattest.cxx
// An interactive test program for the statistician class


#include <cctype>    // Provides toupper
#include <iomanip>   // Provides setw to set the width of an output
#include <iostream>  // Provides cout, cin
#include <cstdlib>   // Provides EXIT_SUCCESS
#include "stats.h"
using namespace CISP430_A1;
using namespace std;

// PROTOTYPES of functions used in this test program:
void print_menu( );
// Postcondition: A menu of choices for this program has been written to cout.
// Library facilties used: iostream.h

char get_user_command( );
// Postcondition: The user has been prompted to enter a one character command.
// A line of input (with at least one character) has been read, and the first
// character of the input line is returned.

double get_number( );
// Postcondition: The user has been prompted to enter a real number. The
// number has been read, echoed to the screen, and returned by the function.

void print_values(const statistician& s);
// Postcondition: The length, sum, minimum, mean, and maximum of s have been
// written to cout, using a field width of 10 for each of these values.
// (If length is zero, then minimum, mean, and maximum are not written.)

int main( )
{
    statistician s1, s2, s3;  // Three statisticians for us to play with
    char choice;              // A command character entered by the user
    double x;                 // Value for multiplication x*s1

    cout << "Three statisticians s1, s2, and s3 are ready to test." << endl;

}

我编译了stats.cpp和HelloWorldII.cpp,如下所示:

gcc -c stats.cpp
gcc -c HelloWorldII.cpp

然后尝试将它们编译成可执行文件,如下所示:

gcc -omyprogram stats.o HelloWorldII.cpp

这又给了我这个错误:

Compile error for gcc -omyprogram stats.o HelloWorldII.cpp

我的构造函数有问题吗?语法错误?我在图书馆里遗漏了什么吗?

另外我可以在命令提示符下输入什么来显示我安装的OS X,gcc和其他东西的版本?也许这将有助于确定问题。

c++ bash macos terminal
1个回答
0
投票

您需要使用g++而不是gcc来编译和链接c ++代码。

© www.soinside.com 2019 - 2024. All rights reserved.