scope issue in reading source code from files

What am I getting the following error for the below code? It does have OpenGL calls i it but I believe it is still mostly a c++ problem -- why is it (textfileread) not in scope:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
('textFileRead' error below)

#include <cstdio>
#include <iostream>
#include <cstdlib>

#include <GL/glew.h>
#include <GL/glfw3.h>
#include <windows.h>
void setShaders() {

    GLuint v, g, f;
    char *vs,*gs, *fs;

    // Create shader handlers
    v = glCreateShader(GL_VERTEX_SHADER);
    g = glCreateShader(GL_GEOMETRY_SHADER);
    f = glCreateShader(GL_FRAGMENT_SHADER);
//*****HERE******
    // Read source code from files
    vs = textFileRead("shader.vtx");
    gs = textFileRead("example.geom");
    fs = textFileRead("shader.frg");

    const char * vv = vs;
    const char * gg = gs;
    const char * ff = fs;

    // Set shader source
    glShaderSource(v, 1, &vv,NULL);
    glShaderSource(g, 1, &gg,NULL);
    glShaderSource(f, 1, &ff,NULL);

    free(vs);free(gs);free(fs);

    // Compile all shaders
    glCompileShader(v);
    glCompileShader(g);
    glCompileShader(f);

    // Create the program
    GLuint p = glCreateProgram();

    // Attach shaders to program
    glAttachShader(p,v);
    glAttachShader(p,g);
    glAttachShader(p,f);

    // Link and set program to use
    glLinkProgram(p);
    glUseProgram(p);
}
int main()
{

	setShaders();
	return 0;
}


..\src\main.cpp: In function 'void setShaders()':
..\src\main.cpp:26:35: error: 'textFileRead' was not declared in this scope
     vs = textFileRead("shader.vtx");

Last edited on
It doesn't look like you have declared a function named textFileRead.
why is it (textfileread) not in scope


A better question would be, why do you think textFileRead should be in scope?

Is it possibly declared in a header that you forgot to include?
Great question. No forgotten header. I tried a different approach (see below) where the function is put in another .cpp file by the author. I admit my weakness in knowledge about using multiple c++ files together AND the out-of-scope error I got with this example too:

main.cpp
1
2
3
4
5
6
7
int main()
{
	std::cout<<"main start"<<std::endl;

	std::cout<<"Calling shader files"<<std::endl;
	ReadShaderFileToMemory("shader.vert");
}


In function 'int main()':
..\src\main.cpp:18:38: error: 'ReadShaderFileToMemory' was not declared in this scope
  ReadShaderFileToMemory("shader.vert");

load_shader_from_file.cpp
1
2
3
4
5
6
const char* ReadShaderFileToMemory(const char* filePath)
{
	const char * shaderFileBuffer = NULL;

	std::ifstream inSdrFileStream(filePath);
	if(inSdrFileStream)


ReadShaderFile is in a .cpp. I don't know how to handle (2) of them. I am pretty sure my out-of-scope errors are coming from incorrect implementation.

Thank you for your time.
Last edited on
ReadShaderFile is in a .cpp. I don't know how to handle (2) of them. I am pretty sure my out-of-scope errors are coming from incorrect implementation.


As mentioned by others, you are missing an include.

Normally, one has to include the header file that belongs to the cpp file that has the function / class that you want to use. That is how we handle multiple cpp / header files.

Is this function part of the API? I am guessing it isn't.

If not then #include "load_shader_from_file.h"

Edit:

I guess you are going to have to learn how to use multiple files, I am fairly sure there will be a limit to how much you can or should do with everything in one file. I would also recommend getting hold of a good tutorial, or even buy a book.
Last edited on
What do you use? IDE / make file / batch file?
Thanks IDM. Ok you confirmed the normative state that must include an include with any cpp, and for that there are no substitutions permitted. Which leaves me scratching my head as to why the author didn't include a .h with the .cpp. It is true it is not part of an API. What I can do is just write a .h corresponding to the .cpp and see what happens there.

Thomas I am using Eclipse Luna IDE, minGW.
Last edited on
Hi,

The good news is that the function I wrote for the missing .h file seems to enable all of the missing parts to talk together. However, I still am having difficulty loading the shader file but the function to load it seems to be doing something:

main start
Calling shader files
Shader File: Error. Not found!


new .h file (also included to my main.cpp):

1
2
3
4
5
6
#ifndef LOAD_SHADER_FROM_FILE_HPP_
#define LOAD_SHADER_FROM_FILE_HPP_

const char* ReadShaderFileToMemory(const char* filePath);

#endif /* LOAD_SHADER_FROM_FILE_HPP_ */ 


load_shader_from_file.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<windows.h>
#include <fstream>

const char* ReadShaderFileToMemory(const char* filePath)
{
	const char * shaderFileBuffer = NULL;

...
...
...
..
else
	{
		std::cout << "Shader File: Error. Not found!" << std::endl;
		return NULL;
	}

	return shaderFileBuffer;
}

main.cpp
1
2
3
4
5
6
int main()
{
	std::cout<<"main start"<<std::endl;

	std::cout<<"Calling shader files"<<std::endl;
	ReadShaderFileToMemory("shader.frag");


Basically it seems it can't find the file. Its in the same directory as the .cpp & .h files.
Last edited on
Where is the .exe file?
The file must be in the same directory as the .exe.
.exe file is in the debug folder
the shader.frag & shader.vert are both there as well, confirmed
Where is the .exe file?
The file must be in the same directory as the .exe.

Not necessarily, it must be in the current working directory. You may be able to see the location by using these directions. If all else fails you should be able to set the working directory to the proper location.
Topic archived. No new replies allowed.