我试图使用标准C库的一些功能,但我得到了这个错误:没有从std :: string到int的合适转换.我刚从C学习C++.请不要用困难的术语解释这个问题.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main(void)
{
string s1{ "Hello" };
bool what{ isalnum(s1) };
return 0;
}
Run Code Online (Sandbox Code Playgroud) 使用 SDL2 和 SDL2_image 尝试一些 C++ 和 OpenGL,基于http://open.gl
一旦到达 glGenTextures 调用,我就会收到主题错误。我的大多数搜索都提到尚未创建 gl 上下文。我已经成功使用了多次 gl 调用。这是我的主要内容。所以
int main(int argc, const char * argv[]) {
uniforms = std::vector<GLuint>();
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
glewExperimental = GL_TRUE;
glewInit();
SDL_Event windowEvent;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo;
glGenBuffers(1, &vbo);
GLuint ebo;
glGenBuffers(1, &ebo);
setupVertices(vbo);
setupElementBuffer(ebo);
GLuint fragmentShader;
GLuint vertexShader;
GLuint shaderProgram = compileShaders(vertexShader, fragmentShader); …Run Code Online (Sandbox Code Playgroud) 如何在C++中减少if语句链?
if(x == 3) {
a = 9876543;
}
if(x == 4) {
a = 987654;
}
if(x == 5) {
a = 98765;
}
if(x == 6) {
a = 9876;
}
if(x == 7) {
a = 987;
}
if(x == 8) {
a = 98;
}
if(x == 9) {
a = 9;
}
Run Code Online (Sandbox Code Playgroud)
这是示例代码.
在这种特殊情况下,为什么我必须在基类中定义非纯虚方法以避免链接器错误?
这会产生链接器错误:
class A
{
public:
virtual ~A(){}
virtual void foo() = 0;
virtual void bar();
};
class B : public A
{
public:
void foo()
{
}
void bar()
{
}
};
int main()
{
B b;
}
Run Code Online (Sandbox Code Playgroud)
输出:
/tmp/cc5E8Tit.o: In function `A::~A()':
:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0x13): undefined reference to `vtable for
A' /tmp/cc5E8Tit.o:(.rodata._ZTI1B[_ZTI1B]+0x10): undefined reference
to `typeinfo for A' collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
但如果我在类中定义 bar 方法,A它链接正常:
class A
{
public:
virtual ~A(){}
virtual void foo() = …Run Code Online (Sandbox Code Playgroud) 我已经搜索了我的问题的解决方案,但没有找到答案.以下代码仅用于学习目的.我想在此向量中擦除值为3的所有元素:
std::vector<int> v{1,1,2,2,2,3,3,4,4};
Run Code Online (Sandbox Code Playgroud)
我试着像这样解决它:
//只是代码的摘录
iterator erase(T const& elem) {
return v.erase(std::remove(v.begin(), v.end(), elem), v.end());
}
Run Code Online (Sandbox Code Playgroud)
但是在我的测试用例中,我希望迭代器指向值4,但在我的代码中它指向值3.它只从我的向量中移除前3个.
如何删除值"3",以便迭代器返回4?
非常感谢您的帮助!
我正在为我的班级编写单元测试,并希望使用私有的方法.我正在做这样的事情:
class MyClass {
Myclass() {}
~MyClass() {}
#ifdef TESTING
public:
#else
private:
#endif
void MyMethod1();
void MyMethod2();
};
Run Code Online (Sandbox Code Playgroud)
如果TESTING已定义,我会更改类签名以使所有内容都公开.在我的测试代码中,我只是这样做:
#define TESTING
#include "MyClass.h"
void MyTestMethod()
{
MyClass mc;
mc.MyMethod1(); // Now I can access MyMethod1
}
Run Code Online (Sandbox Code Playgroud)
所以它只在我的测试文件中公开而不是其他任何地方.我的测试可执行文件看到一个标题,描述了一个全部公开的类.该类的代码实际上是在其他地方构建的(TESTING未定义的地方),因此当测试项目链接到库时,范围将有所不同.
这有可能破坏任何东西吗?我担心如果标题与目标文件中实际生成的标题不同,这可能会改变vtable位置或编译器和链接器期望.
我有这个代码:
Console.Write("Red = ");
var input = Console.ReadLine();
if (!Double.TryParse(input, out red))
{
Console.WriteLine("You have not entered an appropriate value!");
}
Console.Write("Green = ");
input = Console.ReadLine();
if (!Double.TryParse(input, out green))
{
Console.WriteLine("You have not entered an appropriate value!");
}
Console.Write("Green = ");
input = Console.ReadLine();
if (!Double.TryParse(input, out green))
{
Console.WriteLine("You have not entered an appropriate value!");
}
Run Code Online (Sandbox Code Playgroud)
我想更换相同的部件.
我创造了课程
public class ConverNumber
{
public string InputString { get; private set; }
public double Number { get; private set; …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main()
{
short i = 90L;
long j = 90;
printf("%d", sizeof(i));
printf("%d\n", sizeof(j));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我只是读到如果我们把L放在一个整数值的末尾,那么实际上它得到的存储量等于long值,所以基本上他们的意思是说这个,就像我计算两个变量的大小一样,它是不同的,如果它实际上占用了一个长变量的大小,那么显然它会显示大小为4个字节,但它只根据短数据类型的大小显示大小,那么什么是背后的原因.
这个值是否被截断但是当我修改i的值时,它只将它打印为90,所以一切都发生了.
c++ ×6
c ×1
c# ×1
c++11 ×1
if-statement ×1
numbers ×1
opengl ×1
pure-virtual ×1
sdl ×1
sdl-2 ×1
unit-testing ×1
vector ×1