Quick question about nullptr

Hey guys, so I am trying to set a variable to nullptr. I have two files for this project one is a .cpp the other is a .h In the .h file nullptr is not recognized, tells me its an undeclared variable. But in the .cpp file it works fine. Is there a way to get the nullptr to work in the .h file? also what is the reason it dosent work in the .h file?

Hi,

You haven't provided enough context. How have you declared your variables, and how did you assign them?

Best if we can look at the code :+)

Do you have c++11 standard set for your compiler?
Last edited on
no I dont believe c++11 is standard. This is all from the .h file where nullptr is not being recognized

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
#ifndef LAB3_H
#define LAB3_H

#include <iostream>
#include <string>
#include <algorithm>  // used for sort
#include <vector>  // for vector
#include <sstream>
#include <fstream>
using namespace std;

// this is the function where I'm trying to use nullptr
void Wine::print() 
{
	cout << endl;
	cout << "Wine Name: " << wineName << endl;
	cout << "Wine Type: " << wineType << endl;
	cout << "  Vintage: " << vintage << endl; 
	cout << "    Score: " << score << endl;
	cout << "    Price: $" << price  << endl;
	if (winery == nullptr)
	{
		cout << "Winery not found" << endl;
		cout << endl;
	}
	else {
	winery->print_name();
	winery->print_add();
	cout << endl;
	}
}
no I dont believe c++11 is standard.


nullptr is a c++11 feature, you need to have c++11 enabled in your compiler in order to use it or any other c++11 feature.

You should be able to set that option somewhere in your IDE.
but then why would it work in a different file of the same project?
but then why would it work in a different file of the same project?


I don't know, are you using a MS compiler?

Btw, that code belongs in a cpp file not a header file.
Topic archived. No new replies allowed.