0

我可以将 C++ 代码重命名为 .mm 文件并将项目中的所有 .m 文件更改为 .mm 但是在更改为 .mm 后,其中一个 .m 文件中有一个令人困惑的错误

         CFDictionaryRef routeChangeDictionary = inPropertyValue;

它说不能用“const void*”类型的值初始化变量类型“CFDictionaryRef(又名“const_CFDictionary*”)

将所有内容重命名为 .mm 后我不知道这样的错误

无论如何,我有原始的 .CPP 文件 MeterTable.h

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


 class MeterTable
 {
public:

MeterTable(float inMinDecibels = -80., size_t inTableSize = 400, float inRoot = 2.0);   
~MeterTable();

float ValueAt(float inDecibels)
{
    if (inDecibels < mMinDecibels) return  0.;
    if (inDecibels >= 0.) return 1.;
    int index = (int)(inDecibels * mScaleFactor);
    return mTable[index];
}
 private:
float   mMinDecibels;
float   mDecibelResolution;
float   mScaleFactor;
float   *mTable;
  };

MeterTable.CPP #include "MeterTable.h"

 inline double DbToAmp(double inDb)
 {
  return pow(10., 0.05 * inDb);
 }

 MeterTable::MeterTable(float inMinDecibels, size_t inTableSize, float inRoot)
: mMinDecibels(inMinDecibels),
mDecibelResolution(mMinDecibels / (inTableSize - 1)), 
mScaleFactor(1. / mDecibelResolution)
 {
if (inMinDecibels >= 0.)
{
    printf("MeterTable inMinDecibels must be negative");
    return;
}

mTable = (float*)malloc(inTableSize*sizeof(float));

double minAmp = DbToAmp(inMinDecibels);
double ampRange = 1. - minAmp;
double invAmpRange = 1. / ampRange;

double rroot = 1. / inRoot;
for (size_t i = 0; i < inTableSize; ++i) {
    double decibels = i * mDecibelResolution;
    double amp = DbToAmp(decibels);
    double adjAmp = (amp - minAmp) * invAmpRange;
    mTable[i] = pow(adjAmp, rroot);
}

}

   MeterTable::~MeterTable()
{
free(mTable);
}

将 C++ 文件重写为真正的 Object C 类的任何想法?它很短。

4

1 回答 1

0

当我混合使用 C++ 和 Objective-C 代码时,我遇到了非常难以追踪的问题。如果您从 XCode 中的属性将文档设置为 Objective C++,请确保对调用 C++ 代码的 Objective-C 文件执行相同操作,并且 #include 所需的 C++ 头文件。

我以前有过这个,它会收到最奇怪的错误消息。如果不是这种情况,很抱歉在这里混淆了答案。有时,当有多个头文件(包括 C++ 类引用)时,追踪起来会变得更加困难。将所有文件(如果不是大型项目)设置为“Objective C++”不会有什么坏处。

于 2012-10-16T23:53:15.923 回答