我正在编写一些代码,我需要一个类变量,它是一个静态int数组.我明白我可以在头文件中用这样的东西做这个,啊:
#ifndef A_H_
#define A_H_
class A
{
public:
static const int a[];
};
const int A::a[] = {1,2};
#endif
Run Code Online (Sandbox Code Playgroud)
如果我将此标题只包含在另一个文件中,如下所示,main.cpp:
#include "A.h"
#include <iostream>
using namespace std;
int main()
{
A myA;
cout << "0: " << myA.a[0] << endl;
cout << "1: " << myA.a[1] << endl;
}
Run Code Online (Sandbox Code Playgroud)
但是假设我需要我的A类有点复杂,我想要一个A.cpp文件.我将保持我的main.cpp文件相同,但然后更改如下(我刚刚添加了一个函数,printA):
#ifndef A_H_
#define A_H_
class A
{
public:
void printA() const;
static const int a[];
};
const int A::a[] = {1,2};
#endif
Run Code Online (Sandbox Code Playgroud)
然后在文件A.cpp中:
#include "A.h"
#include <iostream>
using namespace …Run Code Online (Sandbox Code Playgroud) 我试图围绕如何适当地使用VAO进行实例化渲染(特别是在Qt 5.2中,使用OpenGL 3.3).我的理解是VAO保存了VBO和相关属性的状态,这样您就不必担心在绘图时绑定和启用所有内容,只需绑定VAO即可.但是通过实例化,您经常会有多个VBO.你如何解决所有需要绑定它们的问题?或者我只需要为每个顶点数据和每个实例数据使用一个VBO?
我一直在看几个教程,例如:http://ogldev.atspace.co.uk/www/tutorial33/tutorial33.html
在我看来,他所做的就是使用VAO作为他的每个顶点数据而不是他的每个实例数据.我尝试用基于Qt的代码做同样的事情,它对我不起作用(可能是因为我不完全理解它是如何工作的......当绘图发生时,他的实例数据是否仍然需要绑定?)
一些虚拟代码...这有点傻,我只是绘制一个两个三角形的实例,透视矩阵作为每个实例属性.
glwindow.cpp:
#include "glwindow.h"
#include <QColor>
#include <QMatrix4x4>
#include <QVector>
#include <QVector3D>
#include <QVector4D>
#include <QDebug>
GLWindow::GLWindow(QWindow *parent)
: QWindow(parent)
, _vbo(QOpenGLBuffer::VertexBuffer)
, _matbo(QOpenGLBuffer::VertexBuffer)
, _context(0)
{
setSurfaceType(QWindow::OpenGLSurface);
}
GLWindow::~GLWindow()
{}
void GLWindow::initGL()
{
setupShaders();
_program->bind();
_positionAttr = _program->attributeLocation("position");
_colourAttr = _program->attributeLocation("colour");
_matrixAttr = _program->attributeLocation("matrix");
QVector<QVector3D> triangles;
triangles << QVector3D(-0.5, 0.5, 1) << QVector3D(-0.5, -0.5, 1) << QVector3D(0.5, -0.5, 1);
triangles << QVector3D(0.5, 0.5, 0.5) << QVector3D(-0.5, -0.5, 0.5) << QVector3D(0.5, -0.5, …Run Code Online (Sandbox Code Playgroud) 我有一个基于Qt 4.8的应用程序,该应用程序在Windows 7上运行,并使用Qt Assistant来显示文档。我经常对Qt Assistant感到头疼,因为我每次更新文档时都需要删除缓存的文件(我一直在阅读各种Qt更新中已解决了缓存问题,但问题似乎一直存在)。当我从应用程序内部或直接从命令行(带有assistant.exe -collectionFile myapp.qhc)启动Assistant时,就会发生这种情况。
当我将应用程序分发给用户时,这是一个主要问题。期望他们删除其系统上的缓存文件是不对的。
在Qt文档中找不到有关如何清除用户缓存的帮助文件的任何内容。有什么我想念的吗?
我也已经为Linux编译了我的应用程序,并且那里似乎没有相同的问题。这只是Windows。
我正在使用guava的ClassPath来检索可用的类.使用Java 8,这很好用,但我正在迁移到Java 10,现在它不起作用.我已经尝试了guava 24.1(Maven Central上可用的最新jar文件)和github上的最新资源(截至2018年4月27日) - 大概是或多或少的番石榴25.0,因为他们昨天发布了它.
我可以检索一些类,但任何java.*包中的任何内容都无法显示.这实际上是一个错误,还是我使用Java 10错误(即模块化意味着我需要做一些特别的事情)?我在Java 9中看到了相同的症状.我尝试使用Java 10(或9)构建和运行以及使用Java 8构建并使用Java 10(或9)运行,这两种行为都是相同的.
我已经建立了一个虚拟的例子,在封装两个类com.just.me和com.just.another:
package com.just.me;
import com.google.common.reflect.ClassPath;
import com.just.another.Dummy;
import java.lang.String;
import java.util.Collections;
import java.util.List;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
List<String> dummyList = Collections.emptyList();
printClassCount("com.just.me", Main.class);
printClassCount("com.just.another", Main.class);
printClassCount("com.just", Main.class);
printClassCount("com.google", Main.class);
printClassCount("java.lang", Main.class);
printClassCount("java.util", Main.class);
printClassCount("java", Main.class);
}
private static void printClassCount(String packageName, Class classForClassLoader) {
System.out.println("Number of toplevel classes in " + packageName …Run Code Online (Sandbox Code Playgroud) 我正在将应用程序迁移到Java 10.我们的应用程序通常使用JRE运行,但我们允许用户通过捆绑tools.jar和使用反射来JavacTool按需加载实例来编译自己的自定义代码.我们的方法如下:
public static JavaCompiler getJavaCompiler() {
String toolJarName = "tools.jar";
File file = getResourceForClass("tools.jar");
try {
file = file.getCanonicalFile();
} catch (IOException ignore) {
}
if (!file.exists())
throw new RuntimeException("Can't find java tools file: '"+file+"'");
try {
URL[] urls = new URL[]{ file.toURI().toURL() };
URLClassLoader cl = URLClassLoader.newInstance(urls);
return Class.forName("com.sun.tools.javac.api.JavacTool", false, cl).asSubclass(JavaCompiler.class).newInstance();
} catch (Exception e) {
throw new RuntimeException("Can't find java compiler from '"+file+"': "+e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
这是必要的,因为javax.tools.ToolProvider.getSystemJavaCompiler()从JRE运行时返回null.我们的方法在Java 8中运行良好,但tools.jar在Java 9中被删除了,我需要的类 …
我有一个相当大的项目(4272 .o 文件),但无法将其与 GNU Make 链接。我遇到了make: /bin/sh: Argument list too long。这是一个使用 qmake 生成 makefile 的 Qt 5 项目。
我知道有很多关于此的问题,但我不知道如何将任何解决方案应用于我的问题。我也不完全确定为什么我在链接步骤中遇到这个问题。我得到的错误是:
make: /bin/sh: Argument list too long
用于链接我的项目的 makefile 条目如下所示:
build/debug/my_target/my_target: $(OBJECTS)
@test -d build/debug/my_target/ || mkdir -p build/debug/my_target/
$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)
Run Code Online (Sandbox Code Playgroud)
它扩展到类似:
@echo linking /build/debug/my_target/my_target && clang++ -ccc-gcc-name g++ -lc++ -L/path/to/licensing/lib -Wl,-rpath,/path/to/qt/lib -Wl,-rpath-link,/path/to/qt/lib -o build/debug/my_target/my_target build/debug/my_target/obj/object1.o build/debug/my_target/obj/object2.o ... build/debug/my_target/obj/object4272.o ... [ a bunch of moc_X.o ] ... [ a bunch of libs ] …Run Code Online (Sandbox Code Playgroud) 我有一个程序不能在Mac上编译(gcc 4.2.1,Apple Inc. build 5658),但似乎在Linux上没有问题(gcc 4.7.2).当我尝试编写一个带有包含STL向量的STL向量的成员函数的类时,我得到了上述错误:
test.h:
#ifndef TEST_H_
#define TEST_H_
#include <vector>
#include <utility>
#include <string>
class testclass
{
public:
void printcontent(const std::vector<std::string> & = std::vector<std::string>());
void printother(const std::vector<std::pair<float,float> >& = std::vector<std::pair<float,float> >());
};
#endif
Run Code Online (Sandbox Code Playgroud)
TEST.CPP:
#include "test.h"
#include <iostream>
using namespace std;
void testclass::printcontent(const vector<string> &v)
{
if (v.empty())
cout << "vector was empty!" << endl;
else
for (unsigned i = 0; i < v.size(); i++)
cout << v.at(i) << endl;
}
void testclass::printother(const vector<pair<float,float> >& v)
{ …Run Code Online (Sandbox Code Playgroud) 我正在使用一些宏,并观察一些奇怪的行为.
我已经将PI定义为常量,然后在宏中将其用于将度数转换为弧度,将弧度转换为度数.弧度到弧度的工作正常,但弧度到度数不会:
piTest.cpp:
#include <cmath>
#include <iostream>
using namespace std;
#define PI atan(1) * 4
#define radians(deg) deg * PI / 180
#define degrees(rad) rad * 180 / PI
int main()
{
cout << "pi: " << PI << endl;
cout << "PI, in degrees: " << degrees(PI) << endl;
cout << "45 degrees, in rad: " << radians(45) << endl;
cout << "PI * 180 / PI: " << (PI * 180 / PI) << endl;
cout << "3.14159 …Run Code Online (Sandbox Code Playgroud)