C++ 构造函数Model::Model的实例与参数列表不匹配
当我想编译我的程序时,我遇到了这个错误。这个程序最初是用xcode编写的,运行良好。我不知道这是否与任何事情,但我试图编译在Visual C++上的Windows。这是标题,这是我在cpp文件中得到错误的地方 型号h:C++ 构造函数Model::Model的实例与参数列表不匹配,c++,C++,当我想编译我的程序时,我遇到了这个错误。这个程序最初是用xcode编写的,运行良好。我不知道这是否与任何事情,但我试图编译在Visual C++上的Windows。这是标题,这是我在cpp文件中得到错误的地方 型号h: #pragma once #include <string> #include <fstream> #include <sstream> #include <iostream> #include <map> #incl
#pragma once
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <map>
#include <vector>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "../SOIL2/SOIL2.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "mesh.h"
using namespace std;
GLint TextureFromFile( const char *path, string directory );
class Model {
public:
Model( GLchar *path ) {
this->loadModel( path );
}
void Draw( Shader shader ) {
for ( GLuint i = 0; i < this->meshes.size( ); i++ ) {
this->meshes[i].Draw( shader );
}
}
private:
vector<Mesh> meshes;
string directory;
vector<Texture> textures_loaded;
void loadModel( string path ) {
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile( path, aiProcess_Triangulate | aiProcess_FlipUVs );
if( !scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode ) {
cout << "ERROR::ASSIMP:: " << importer.GetErrorString( ) << endl;
return;
}
this->directory = path.substr( 0, path.find_last_of( '/' ) );
this->processNode( scene->mRootNode, scene );
}
void processNode( aiNode* node, const aiScene* scene ) {
for ( GLuint i = 0; i < node->mNumMeshes; i++ ) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
this->meshes.push_back( this->processMesh( mesh, scene ) );
}
for ( GLuint i = 0; i < node->mNumChildren; i++ ) {
this->processNode( node->mChildren[i], scene );
}
}
Mesh processMesh( aiMesh *mesh, const aiScene *scene ) {
vector<Vertex> vertices;
vector<GLuint> indices;
vector<Texture> textures;
for ( GLuint i = 0; i < mesh->mNumVertices; i++ ) {
Vertex vertex;
glm::vec3 vector;
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertex.Position = vector;
vector.x = mesh->mNormals[i].x;
vector.y = mesh->mNormals[i].y;
vector.z = mesh->mNormals[i].z;
vertex.Normal = vector;
if( mesh->mTextureCoords[0] ) {
glm::vec2 vec;
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.TexCoords = vec;
} else {
vertex.TexCoords = glm::vec2( 0.0f, 0.0f );
}
vertices.push_back( vertex );
}
for ( GLuint i = 0; i < mesh->mNumFaces; i++ ) {
aiFace face = mesh->mFaces[i];
for ( GLuint j = 0; j < face.mNumIndices; j++ ) {
indices.push_back( face.mIndices[j] );
}
}
if( mesh->mMaterialIndex >= 0 ) {
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
vector<Texture> diffuseMaps = this->loadMaterialTextures( material, aiTextureType_DIFFUSE, "texture_diffuse" );
textures.insert( textures.end( ), diffuseMaps.begin( ), diffuseMaps.end( ) );
vector<Texture> specularMaps = this->loadMaterialTextures( material, aiTextureType_SPECULAR, "texture_specular" );
textures.insert( textures.end( ), specularMaps.begin( ), specularMaps.end( ) );
}
return Mesh( vertices, indices, textures );
}
vector<Texture> loadMaterialTextures( aiMaterial *mat, aiTextureType type, string typeName ) {
vector<Texture> textures;
for ( GLuint i = 0; i < mat->GetTextureCount( type ); i++ ) {
aiString str;
mat->GetTexture( type, i, &str );
GLboolean skip = false;
for ( GLuint j = 0; j < textures_loaded.size( ); j++ ) {
if( textures_loaded[j].path == str ) {
textures.push_back( textures_loaded[j] );
skip = true;
break;
}
}
if( !skip ) {
Texture texture;
texture.id = TextureFromFile( str.C_Str( ), this->directory );
texture.type = typeName;
texture.path = str;
textures.push_back( texture );
this->textures_loaded.push_back( texture );
}
}
return textures;
}
};
GLint TextureFromFile( const char *path, string directory ) {
string filename = string( path );
filename = directory + '/' + filename;
GLuint textureID;
glGenTextures( 1, &textureID );
int width, height;
unsigned char *image = SOIL_load_image( filename.c_str( ), &width, &height, 0, SOIL_LOAD_RGB );
glBindTexture( GL_TEXTURE_2D, textureID );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image );
glGenerateMipmap( GL_TEXTURE_2D );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture( GL_TEXTURE_2D, 0 );
SOIL_free_image_data( image );
return textureID;
}
你有不同的选择 我建议将模型构造函数修改为:
Model(string path ) {
this->loadModel( path );
}
因此,您将收到一个字符串对象,该对象将被传递给加载程序
另一个选项是在对象声明中执行强制转换:
Model earthModel((GLChar*)"resources/models/earth/Earth.obj");
但是我不喜欢它,因为它可能与GLchar和char类型不匹配。您应该粘贴得到的完整错误,但原因很可能是您的第一个
模型
构造函数:它使用GLchar*
而不是const GLchar*
。您得到的确切错误消息是什么?可能是因为您试图将const GLchar*
传递给需要(非const)char*
的构造。对于每个模型:错误C2664'model::model(model&)∶无法将参数1从'const char[37]转换为'GLchar*'sloar系统也存在此错误(活动)E0289没有构造函数的实例“model::model”与sloar系统的参数列表相匹配这些澄清应该放在你的问题中,但是将Model::Model
的签名更改为const GLchar*
应该可以修复这些错误。它修复了这些错误,但我收到了这些警告:严重性代码描述项目文件行抑制状态警告C26495变量“aiVectorKey::mTime”未初始化。始终初始化成员变量(类型.6)。此警告是另一个问题。打开另一个问题。@I:当你问一个新问题时,请确保投入一些时间来润色它。确保大写字母正确,纠正打字错误,包括相关代码(包括正确的代码格式),并在问题本身中提供准确的错误信息。
Model earthModel((GLChar*)"resources/models/earth/Earth.obj");