我是一个完整的C ++新手。我正在尝试建立Redis连接,并能够使用c ++方法将密钥设置为redis。 C ++代码很长,因此Im仅包括相关部分:
webpage.h
#ifndef WEBPAGE_H
#define WEBPAGE_H
#包括
#包括
#包括
#包括
#include“ cookiejar.h”
#include“ redispp.h”
类Config;
类CustomPage;
class WebpageCallbacks;
NetworkAccessManager类;
QWebInspector类;
WebPage类:公共QObject,公共QWebFrame :: PrintCallback
{
Q_OBJECT
Q_PROPERTY(QString标题为READ标题)
Q_PROPERTY(QString frameTitle读取frameTitle)
Q_PROPERTY(QString内容READ内容WRITE setContent)
Q_PROPERTY(QString frameContent READ frameContent WRITE setFrameContent)
Q_PROPERTY(QString url READ url)
Q_PROPERTY(QString frameUrl READ frameUrl)
Q_PROPERTY(布尔加载READ加载)
Q_PROPERTY(int loadingProgress读取LOAD loadingProgress)
Q_PROPERTY(布尔canGoBack读取canGoBack)
Q_PROPERTY(bool canGoForward READ canGoForward)
Q_PROPERTY(QString plainText READ plainText)
Q_PROPERTY(QString framePlainText读取framePlainText)
Q_PROPERTY(QString libraryPath READ libraryPath WRITE setLibraryPath)
Q_PROPERTY(QString offlineStoragePath读取offlineStoragePath)
Q_PROPERTY(int offlineStorageQuota读取offlineStorageQuota)
Q_PROPERTY(QVariantMap viewportSize READ viewportSize WRITE setViewportSize)
Q_PROPERTY(QVariantMap paperSize READ paperSize WRITE setPaperSize)
Q_PROPERTY(QVariantMap clipRect READ clipRect WRITE setClipRect)
Q_PROPERTY(QVariantMap scrollPosition READ scrollPosition WRITE setScrollPosition)
Q_PROPERTY(布尔导航锁定READ导航锁定WRITE setNavigationLocked)
Q_PROPERTY(QVariantMap customHeaders READ customHeaders WRITE setCustomHeaders)
Q_PROPERTY(qreal zoomFactor READ zoomFactor WRITE setZoomFactor)
Q_PROPERTY(QVariantList cookie读取cookie写入setCookies)
Q_PROPERTY(QString windowName READ windowName)
Q_PROPERTY(QObjectList页面为READ页面)
Q_PROPERTY(QStringList pagesWindowName READ pagesWindowName)
Q_PROPERTY(布尔ownsPages读取ownsPages WRITE setOwnsPages)
Q_PROPERTY(QStringList框架名称读取框架名称)
Q_PROPERTY(QString frameName READ frameName)
Q_PROPERTY(int framesCount READ framesCount)
Q_PROPERTY(QStringfocusedFrameName读取focusedFrameName)
Q_PROPERTY(QObject * cookieJar读取cookieJar写setCookieJarFromQObject)
Q_PROPERTY(QStringList captureContent读取captureContent写入setCaptureContent)
上市:
WebPage(QObject *父级,const QUrl&baseUrl = QUrl());
虚拟〜WebPage();
QWebFrame * mainFrame();
QString content()const;
QString frameContent()const;
void setContent(const QString&content);
void setFrameContent(const QString&content);
QString title()const;
QString frameTitle()const;
QString url()const;
QString frameUrl()const;
bool loading()const;
int loadingProgress()const;
QString plainText()const;
QString framePlainText()const;
QString libraryPath()const;
void setLibraryPath(const QString&dirPath);
QString offlineStoragePath()const;
int offlineStorageQuota()const;
void setViewportSize(const QVariantMap&size);
QVariantMap viewportSize()const;
void setClipRect(const QVariantMap&size);
QVariantMap clipRect()const;
void setScrollPosition(const QVariantMap&size);
QVariantMap scrollPosition()const;
void setPaperSize(const QVariantMap&size);
QVariantMap paperSize()const;
setNavigationLocked(布尔锁);
bool navigationLocked();
void setCustomHeaders(const QVariantMap&headers);
QVariantMap customHeaders()const;
}
webpage.cpp
#包括“ webpage.h”
#包括“ networkaccessmanager.h”
#包括“ utils.h”
#包括“ config.h”
#包含“ consts.h”
#包括“ callback.h”
#包括“ cookiejar.h”
#包括“ system.h”
#包括“ redispp.h”
#ifdef Q_OS_WIN32
#定义STDOUT_FILENAME“ / dev / stdout”
#定义STDERR_FILENAME“ / dev / stderr”
/ **
* @class CustomPage
* /
类CustomPage:public QWebPage {
Q_OBJECT
无效的WebPage :: connectRedis()
{
this-> redis = redispp :: Connection conn(“ 127.0.0.1”,“ 6379”,“密码”,false);
}
无效的WebPage :: redisSet(const QString&key,const QString&value)
{
this-> redis.set(key,value);
}
int WebPage :: offlineStorageQuota()const
{
返回m_customWebPage-> settings()-> offlineStorageDefaultQuota();
}
无效的WebPage :: showInspector(const int port)
{
m_customWebPage-> settings()-> setAttribute(QWebSettings :: DeveloperExtrasEnabled,true);
m_inspector =新的QWebInspector;
m_inspector-> setPage(m_customWebPage);
如果(端口== -1){
m_inspector-> setVisible(true);
}其他{
m_customWebPage-> setProperty(“ _ q_webInspectorServerPort”,端口);
}
}
无效的WebPage :: applySettings(const QVariantMap&def)
{
QWebSettings * opt = m_customWebPage-> settings();
opt-> setAttribute(QWebSettings :: AutoLoadImages,def [PAGE_SETTINGS_LOAD_IMAGES] .toBool());
opt-> setAttribute(QWebSettings :: JavascriptEnabled,def [PAGE_SETTINGS_JS_ENABLED] .toBool());
opt-> setAttribute(QWebSettings :: XSSAuditingEnabled,def [PAGE_SETTINGS_XSS_AUDITING] .toBool());
opt-> setAttribute(QWebSettings :: LocalContentCanAccessRemoteUrls,def [PAGE_SETTINGS_LOCAL_ACCESS_REMOTE] .toBool());
opt-> setAttribute(QWebSettings :: WebSecurityEnabled,def [PAGE_SETTINGS_WEB_SECURITY_ENABLED] .toBool());
opt-> setAttribute(QWebSettings :: JavascriptCanOpenWindows,def [PAGE_SETTINGS_JS_CAN_OPEN_WINDOWS] .toBool());
opt-> setAttribute(QWebSettings :: JavascriptCanCloseWindows,def [PAGE_SETTINGS_JS_CAN_CLOSE_WINDOWS] .toBool());
如果(定义包含(PAGE_SETTINGS_USER_AGENT)){
m_customWebPage-> m_userAgent = def [PAGE_SETTINGS_USER_AGENT] .toString();
}
if(def.contains(PAGE_SETTINGS_USERNAME)){
m_networkAccessManager-> setUserName(def [PAGE_SETTINGS_USERNAME] .toString());
}
如果(定义包含(PAGE_SETTINGS_PASSWORD)){
m_networkAccessManager-> setPassword(def [PAGE_SETTINGS_PASSWORD] .toString());
}
if(def.contains(PAGE_SETTINGS_MAX_AUTH_ATTEMPTS)){
m_networkAccessManager-> setMaxAuthAttempts(def [PAGE_SETTINGS_MAX_AUTH_ATTEMPTS] .toInt());
}
如果(定义包含(PAGE_SETTINGS_RESOURCE_TIMEOUT)){
m_networkAccessManager-> setResourceTimeout(def [PAGE_SETTINGS_RESOURCE_TIMEOUT] .toInt());
}
}
QString WebPage :: userAgent()常量
{
返回m_customWebPage-> m_userAgent;
}
WebPage :: setNavigationLocked(布尔锁定)
{
m_navigationLocked =锁定;
}
布尔WebPage :: navigationLocked()
{
返回m_navigationLocked;
}
无效的WebPage :: setViewportSize(const QVariantMap&size)
{
int w = size.value(“ width”)。toInt();
int h = size.value(“ height”)。toInt();
如果(w> 0 && h> 0){
m_customWebPage-> setViewportSize(QSize(w,h));
}
}
QVariantMap WebPage :: viewportSize()const
{
QVariantMap结果;
QSize大小= m_customWebPage-> viewportSize();
result [“ width”] = size.width();
result [“ height”] = size.height();
返回结果;
}
}
编译错误
webpage.cpp:950:11:错误:“网页”中没有名为“ redis”的成员
this-> redis = redispp :: Connection conn(“ 127.0.0.1”,“ 6379”,“密码”,false);
~~~~ ^
webpage.cpp:950:39:错误:函数样式强制转换或类型构造应为'('
this-> redis = redispp :: Connection conn(“ 127.0.0.1”,“ 6379”,“密码”,false);
~~~~~~~~~~~~~~~~~~ ^
webpage.cpp:955:7:错误:'WebPage'中没有名为'redis'的成员
this-> redis.set(key,value);
~~~~ ^
我正在使用的这个c ++ Redis库是
https://github.com/brianwatling/redispp
注意**
*我尝试使用Redispp名称空间,例如在示例中,但是连接类将与其他类冲突。
https://github.com/brianwatling/redispp/blob/master/test/multi.cpp
*
整个webpage.cpp文件位于pastebin中。文件超出了StackOverflow字符限制
http://pastebin.com/fUhRwy0R
任何帮助,将不胜感激。
最佳答案
您的类称为CustomPage
,但是由于某种原因,您正在WebPage
中实现函数。
将函数定义更改为此:
void connectRedis()
和...
void redisSet(const QString & key, const QString & value)
另外,由于似乎您可能来自Java或C#,所以在C ++中执行操作的标准方法是将类定义与实现分开(分为.h / .hpp和.cpp文件),有关更多信息,请参见here在此。
关于c++ - 尝试使用Redispp在类中设置全局Redis连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31616538/