小编Mat*_*llo的帖子

由于尚未执行的代码显然引发了错误

我正在通过编写将MIDI文件转换为Lilypond源文件的程序来学习c ++ .我的程序由两个主要部分组成:

  • 一个MIDI文件解析器,它创建一个名为MidiFile的对象.
  • 获取MidiFile对象并将其转换为Lilypond源的转换器.

今天我开始对转换器进行编码,当我测试它时发生了一个奇怪的错误:程序在抛出异常后死亡,更具体地说是HeaderError,这意味着MIDI文件中的标题块不是预期的.它似乎并不奇怪,但只有在我的错误代码之后添加一行代码时才出现此错误!我添加了main()函数来更好地解释自己

#include <iostream>
#include "midiToLyConverter.hpp"

int main(){

            // a queue to store notes that have not yet been shut down
    using MidiToLyConverter::Converter::NoteQueue;
            // representation of a note
    using MidiToLyConverter::Converter::Note;
            // the converter class
    using MidiToLyConverter::Converter::Converter;
            // the midifile class
    using Midi::MidiFile;
            // representation of a midi track
    using Midi::MidiTrack;
            // representation of a midi event
    using Midi::MidiEvents::Event;

    Parser::Parser parser = Parser::Parser(); // parser class
    parser.buildMidiFile(); // builds the midi file from a .mid …
Run Code Online (Sandbox Code Playgroud)

c++ memory-leaks exception

6
推荐指数
1
解决办法
127
查看次数

gcc链接器如何在包含文件中起作用

这真的是一个新手问题.我正在学习C,我不明白如何将不同的文件链接在一起.

我有一个标题

/* file2.h */

int increment(int x);
Run Code Online (Sandbox Code Playgroud)

和一个C文件

/* file2.c */

#include "file2.h"

int increment(int x)
{
    return x+1;
}
Run Code Online (Sandbox Code Playgroud)

现在我想要包含标题file1.c以便使用该函数increment.根据我的理解,我必须做以下事情:

/* file1.c*/

#include "file2.h"

int main()
{
    int y = increment(1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试编译整个事情时,使用

gcc -o program file1.c
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息

/tmp/ccDdiWyO.o: In function `main':
file1.c:(.text+0xe): undefined reference to `increment'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

但是,如果我还包括file2.c

/* file1.c*/

#include "file2.h"
#include "file2.c"  /* <--- here it is! */

int main()
{
    int y = …
Run Code Online (Sandbox Code Playgroud)

c linker gcc linker-errors

2
推荐指数
1
解决办法
878
查看次数

有没有办法为类的实例定义[]运算符?

我想定义类来表示矩阵

class matrix:
    def __init__(self, mat):
        self.mat = mat
        self.dim = len(mat)

    @classmethod
    def withDim(matrix, dimension):
        mat = [ [0]*dimension for i in range(dimension)]
        return matrix(mat)
Run Code Online (Sandbox Code Playgroud)

其中mat是列表列表,以表示矩阵

A = | a b |
    | c d |
Run Code Online (Sandbox Code Playgroud)

我可以写下面的内容

A = matrix( [ [a, b], [c, d] ])
Run Code Online (Sandbox Code Playgroud)

我也开始定义一些运算符,比如

def __add__(self, other):
    n = self.dim
    result = self.withDim(n)
    for i in range(n):
        for j in range(n):
            result.mat[i][j] = self.mat[i][j] + other.mat[i][j]
    return result
Run Code Online (Sandbox Code Playgroud)

现在,如果我想访问i, j矩阵中的元素,A我必须这样做 …

python object operators

2
推荐指数
1
解决办法
66
查看次数

标签 统计

c ×1

c++ ×1

exception ×1

gcc ×1

linker ×1

linker-errors ×1

memory-leaks ×1

object ×1

operators ×1

python ×1