Error: variable or field 'xxx' declared void

On running the following codes I'm getting these errors. I'm using Code Blocks 13.12

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
void view(string); //error: variable or field 'view' declared void & error: 'string' was not declared in this scope

void customer()
{
    char ch;
    ch=getche();
	switch(ch)
	{
        case '1':break;
        case '2':view("customer.txt"); //error: 'view' was not declared in this scope
        case '3':break;
        case 'b':main_menu();
	}
}

void view(string fname) //error: variable or field 'view' declared void & error: 'string' was not declared in this scope
{
    string line;
    ifstream myfile (fname);
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )
        {
            cout << line << '\n';
        }
        myfile.close();
    }
    else
    {
        std::cout<<"\nError Can't Open File !";
    }
}

int main()
{
	view("customer.txt");
	return 0;
}
1
2
#include <iostream>
#include <string> 
@MiiNiPaa,
I've included these header files already. Still same Error.
then use qualified name std::string.

Or post complete code so we could test all possibilities.
closed account (1CfG1hU5)
stab in the dark

declare "using namespace std;" before function main

that work?
Last edited on
@jt1 thanx, By using "using namespace std;" all the error are gone. Now I've one new error

Actually I want to open and view a files content through a function. I'm passing filename to a function named 'view()'. My code is

Error: no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::string&)'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void view(string fname)
{
    string line;
    std::ifstream myfile (fname);
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )
        {
            cout << line << '\n';
        }
        myfile.close();
    }
    else
    {
        std::cout<<"\nError Can't Open File !";
    }
}

int main()
{
  view("customer.txt");
}
Turn on C++11 support.
Thanks @MiiNiPaa,
It worked.
Topic archived. No new replies allowed.