Quick question about nullptr

Mar 29, 2016 at 2:38am
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?

Mar 29, 2016 at 2:51am
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 Mar 29, 2016 at 2:52am
Mar 29, 2016 at 3:13am
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;
	}
}
Mar 29, 2016 at 3:24am
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.
Mar 29, 2016 at 3:43am
but then why would it work in a different file of the same project?
Mar 29, 2016 at 3:59am
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.