我正在尝试编写一个简单的程序来查找TagLib(http://developer.kde.org/~wheeler/taglib.html)的工作原理。该程序与代码中的注释一起正常工作。但是当我改变代码时,我得到了这些我无法解决的错误。 这是代码
#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*)}’
答案 0 :(得分:2)
两个版本 - 评论和未注释 - 都是错误的。在Unix上,TagLib需要const char *
个文件名。从QString
获取TagLib::FileRef f(QFile::encodeName(file).constData());
的最佳方法是使用QFile::encodeName()
。所以你应该拥有的是这样的:
{{1}}