【发布时间】:2012-05-14 07:55:59
【问题描述】:
我正在尝试编写一个简单的程序来了解 TagLib (http://developer.kde.org/~wheeler/taglib.html) 的工作原理。该程序可以很好地与代码中的 cmets 配合使用。但是当我更改代码时,我得到了这些我无法解决的错误。 这是代码
#include<iostream>
#include<QString>
//#include "Helper.h"
#include "../../player/Util.h"
#include <taglib/tag.h>
#include <taglib/taglib.h>
#include <taglib/fileref.h>
#include <QString>
#include <boost/filesystem.hpp>
using std::string;
struct MetaData{
string filepath, artist, album, title;
signed int year, track_num, length_ms, bitrate;
};
//MetaData getMetaDataOfFile(QString file)
MetaData getMetaDataOfFile(string file)
{
MetaData md;
//TagLib::FileRef f(TagLib::FileName(file.toUtf8()));
const char * filename = file.c_str();
std::cout<< filename;
TagLib::FileRef f(TagLib::FileName(filename));
//md.filepath = file.toStdString();
md.filepath = file;
//boost::filesystem::path filePath(file.toStdString());
boost::filesystem::path filePath(file);
md.title = filePath.stem().string();
if(f.isNull()) return md;
if(!f.tag()) return md;
if(f.tag()->isEmpty()) return md;
string artist = f.tag()->artist().to8Bit(true);
string album = f.tag()->album().to8Bit(true);
string title = f.tag()->title().to8Bit(true);
uint year = f.tag()->year();
uint track = f.tag()->track();
int bitrate = f.audioProperties()->bitrate() * 1000;
int length = f.audioProperties()->length();
md.album = cnvrtString2FirstUpper(album);
md.artist = cnvrtString2FirstUpper(artist);
md.title = cnvrtString2FirstUpper(title);
//md.filepath = file.toStdString();
md.filepath = file;
md.length_ms = length * 1000;
md.year = year;
md.track_num = track;
md.bitrate = bitrate;
if(md.title.length()==0)
{
//boost::filesystem::path filePath(file.toStdString());
boost::filesystem::path filePath(file);
md.title = filePath.stem().string();
}
return md;
}
int main()
{
using namespace std;
//QString file("/home/vickey/Downloads/song1.mp3");
string file("/home/vickey/Downloads/song1.mp3");
MetaData md = getMetaDataOfFile(file);
return 0;
}
编译时出现此错误
g++ getmd.cpp -o getmd -I /usr/include/qt4/ -I /usr/include/qt4/QtCore/ -ltag -lboost_system -lboost_filesystem -lQtCore -g
getmd.cpp: In function ‘MetaData getMetaDataOfFile(std::string)’:
getmd.cpp:36:10: error: request for member ‘isNull’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:37:11: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:38:10: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:40:23: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:41:22: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:42:22: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:43:19: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
g etmd.cpp:44:20: error: request for member ‘tag’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:45:21: error: request for member ‘audioProperties’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
getmd.cpp:46:20: error: request for member ‘audioProperties’ in ‘f’, which is of non-class type ‘TagLib::FileRef(TagLib::FileName) {aka TagLib::FileRef(const char*)}’
【问题讨论】:
-
当你把代码改成什么??
-
代码中的注释为原代码。注释后面是修改后的代码。
-
尝试在构造函数参数周围多加一对括号:
TagLib::FileRef f((TagLib::FileName(file.toUtf8())));这可以帮助解决 C++ 语法的特殊歧义。 -
奇怪,但现在它正在使用“TagLib::FileRef f(TagLib::FileName(file.c_str()));” .我真的不知道出了什么问题,但现在它工作正常:)
-
使用
c_str()进行文件名转换会因某些编码而失败(它会编译,但不会打开文件)。请参阅下面的答案,了解正确的做事方式。
标签: c++ audio boost metadata taglib