标签: header-files

C++初学者 - 在类中使用类时遇到麻烦

我正在做一个大学项目,我必须在那里实施一个简单的Scrabble游戏.

我有一个player班级(包含分数和玩家的手,形式为a std::string和score类(包含名称和数字(int)分数).

其中一个Player成员函数是Score getScore(),返回该玩家的Score对象.但是,我在编译时遇到以下错误:

player.h(27) : error C2146: syntax error : missing ';' before identifier 'getScore'
player.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(27) : warning C4183: 'getScore': missing return type; assumed to be a member function returning 'int'
player.h(35) : error C2146: …
Run Code Online (Sandbox Code Playgroud)

c++ class return-type header-files

0
推荐指数
1
解决办法
1493
查看次数

编译器如何工作,Makefile在前向声明的情况下发挥作用

我在堆栈溢出中遇到了一个建议,提到了何时使用以及何时不向用户转发声明.

我遇到了这个: -

struct X;              // Forward declaration of X

void f1(X* px) {}      // Legal: can always use a pointer/reference
X f2(int);             // Legal: return value in function prototype
void f3(X);            // Legal: parameter in function prototype
void f4(X) {}          // ILLEGAL: *definitions* require complete types`
Run Code Online (Sandbox Code Playgroud)

在编译时,最后一行说非法将失败.

void f3(X); // Works perfectly fine 
Run Code Online (Sandbox Code Playgroud)

因此,所有头文件(.hh)首先由编译器扫描,然后所有.cc文件寻找语法和语义,我们实际上可以定义
void f3(X);as扫描通过头文件编译器将有关于X的成员函数和成员的想法

c++ header-files

0
推荐指数
1
解决办法
143
查看次数

支持功能声明应该去哪里?

我有一个.cpp源文件,其中包含一些需要公开访问的函数和一些仅在此源文件中使用的支持函数.

我已经将所有这些函数声明放在头文件中,因为我个人觉得查看一个类在一个地方提供的所有内容很有用.但是,我想指出这些函数是否供内部使用,类似于private访问修饰符,但不使用类(它们是独立函数).

一些可能的解决方案:

  • 将私有声明放在源文件中.
  • 将私有声明放在单独的标头中.

这两种解决方案都将公共和私有函数分成了我想避免的单独文件.

c++ header-files private-header

0
推荐指数
1
解决办法
759
查看次数

如何在C++中只包含头文件中的一个符号

我有一个很大的头文件,但在一个程序中,我只需要在该头文件中定义一些特定的变量.

在c ++中有什么办法可以告诉它只包含那个特定的变量吗?

并且,是的,变量必须只在该头文件中.

如果我包含整个头文件(因为我现在正在做),有什么不利之处?

c++ header-files build-definition

0
推荐指数
1
解决办法
88
查看次数

在另一个类中使用类函数

我有一个完美运行的LinkedList.h头文件.我试图使用LinkedList来实现堆栈.问题在于使用此MyStack类中LinkedList类的函数.InsertFront(key)是LinkedList类中的一个函数.现在,当我运行它时,我得到以下2个错误.

'list':未声明的标识符

'.InsertFront'的左边必须有class/struct/union

这两个都引用了以下代码的最后一行:

#include "LinkedList.h"
#include <iostream>
using namespace std;



class MyStack
{
    private:
    //LinkedList list;


    public:
        //Implement the following functions whose purpose should be obvious
        MyStack();
        ~MyStack();
        void Push(int key);
        int Pop();
        bool isEmpty();
        int Size();
        void Display(); // Just show the elements from top of stack to the bottom without removing them
        int Top(); // Return the element at the top of the stack without removing it
};


MyStack::MyStack()
{
    LinkedList list;
}

MyStack::~MyStack()
{

}

void …
Run Code Online (Sandbox Code Playgroud)

c++ stack class linked-list header-files

0
推荐指数
1
解决办法
74
查看次数

在linux中用自己的头文件编译和运行c++程序

这是我第一次制作自己的头文件。我正在尝试在 Ubuntu 上用 C++ 制作一个简单的 Hello World 程序。制作了 3 个文件,如下所示:

//你好.h文件

#ifndef HELLO_H
#define HELLO_H

//my own code

void hello();

#endif
Run Code Online (Sandbox Code Playgroud)

//你好.cpp文件

#include <iostream>
#include "hello.h"

using namespace std;

void hello()
{
    cout << "This line is printed from header.";
}
Run Code Online (Sandbox Code Playgroud)

//main.cpp文件

#include <iostream>
#include "hello.h"

using namespace std;

int main()
{
    cout << "in main" << endl << endl;
    hello();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我试过了

g++ -I /home/Desktop/hello/ -c hello.cpp -o hello.o
Run Code Online (Sandbox Code Playgroud)

编译头文件,此命令有效。

然后,在做的时候

g++ -o main main.cpp
Run Code Online (Sandbox Code Playgroud)

我结束了以下错误:

/tmp/ccb0DwHP.o: …
Run Code Online (Sandbox Code Playgroud)

c++ linux header-files

0
推荐指数
1
解决办法
3万
查看次数

程序仅适用于 .h 文件中的 {} 符号

我有三个文件。为什么程序仅适用于 .h 文件中的 {} 符号?

szereg.c:

    #include "szereg.h"

double szereg(double x, double eps, int *sw, int *M, int *stop){ 

    double s, w;
    int i = 2;
        s=x;
        w=x;
    do{ 
        if(*sw==*M){
            if(fabs(w)<=eps && *sw==*M)
                *stop =3;
            else
                *stop = 1;
            return s;
        }
        w=((-w*x)/i)*(i-1);
        s=s+w;
        *sw+=1;
        i++;
    } 
    while (fabs(w) >= eps); 

    *stop = 2;

    return s; 
}
Run Code Online (Sandbox Code Playgroud)

szereg.h:

    double szereg(){};
Run Code Online (Sandbox Code Playgroud)

主文件:

        #include <stdio.h>
        #include <math.h>
        #include <stdlib.h>
        #include "szereg.h"

        FILE *fw; 
        extern double szereg(); 

        int main(){ 

            int lp, sw=1, M, …
Run Code Online (Sandbox Code Playgroud)

c split header-files code-splitting

0
推荐指数
1
解决办法
45
查看次数

头文件是翻译单元吗?

头文件是翻译单元吗?如果我将 static 关键字添加到头文件中的变量中,我可以在 .c 或 .cpp 文件中调用该变量吗?谢谢。

c c++ file header-files translation-unit

0
推荐指数
1
解决办法
144
查看次数

错误:使用未声明的标识符“类名”

我正在尝试实现一个“时间”类。我已将所有声明放在 Time.h 中,并将实现放在 Time.cpp 中。然后我在一个单独的文件 Test.cpp 中测试它。编译 Time.cpp 文件时,出现此错误error: use of undeclared identifier 'Time'。这是代码,

时间.h

#ifndef _TIME_H_
#define _TIME_H_

#include <iostream>

class Time
{
    public:
        Time();
        
        // extra constructor not provided in part 3
        Time(int hour, int min = 0, int h = 0);
        
        void setTime(int h, int m, int s);

        Time operator+(unsigned int n) const;
        Time& operator+=(unsigned int n);
        Time& operator++(); // prefix version
        Time operator++(int n); // postfix version

        // member functions you have to implement …
Run Code Online (Sandbox Code Playgroud)

c++ class header-files c++11

0
推荐指数
1
解决办法
140
查看次数

C++ 是否包含包含的头文件包含的所有头文件?

在此示例代码中,我有 3 个文件:

testHeader.h:

void hello() {  }
Run Code Online (Sandbox Code Playgroud)

文件1.h:

#include "testHeader.h"
#include <iostream>
void sayHi() { std::cout << "hi" << std::endl; }
Run Code Online (Sandbox Code Playgroud)

文件2.h:

#include "file1.h"
void sayHello() { std::cout << "hello" << std::endl; }
Run Code Online (Sandbox Code Playgroud)

如果file1.hincludestestHeader.h和file2.hincludes file1.h,dotestHeader.h和它的函数在file2.h? 怎么样<iostream>和它的功能是什么?

c++ header-files

0
推荐指数
1
解决办法
214
查看次数