473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Need help with compiler errors for a simple C++ program using strings

My code includes the driver.cpp (main program), Student.h, and Student.cpp files.
Perhaps there is a problem with the implementation of the string _id, _name or the C++ linking library is missing a key ingredient to compile the program and run. (errors after program code)
Expand|Select|Wrap|Line Numbers
  1. // file: driver.cpp (main program)
  2. #include "Student.h"
  3.  
  4. int main()
  5. {
  6.   Student student(101, "Tom Lee");
  7.  
  8.   student.display();
  9.   return 0;
  10. }
  11.  
  12. // file: Student.h
  13.  
  14. #include <iostream>
  15. #include <string>
  16. using namespace std;
  17.  
  18. #ifdef STUDENT_H
  19. #define STUDENT_H
  20.  
  21. class Student
  22. {
  23. private:
  24.   int _id;
  25.   string _name;
  26. public:
  27.   Student();
  28.   Student(int, string);
  29.   ~Student();
  30.   void display();  
  31.  
  32. };
  33. #endif
  34.  
  35. // file: Student.cpp
  36.  
  37. #include <iostream>
  38. #include <string>
  39. using namespace std;
  40.  
  41. #include "Student.h"
  42. //-----------------------------------
  43. Student::Student(){}
  44. //-----------------------------------
  45. Student::Student(int id, string name)
  46. {
  47.   _id = id;
  48.   _name = name;
  49.  
  50. }
  51. //-----------------------------------
  52. Student::~Student(){}
  53. //-----------------------------------
  54. void Student::display()
  55. {
  56.   cout << "ID: " >> _id;
  57.   cout << endl;
  58.   cout << "NAME: " >> _name;
  59.   cout << endl;
  60. }
  61. //-----------------------------------
my errors:
driver.cpp: In function ‘int main()’:
driver.cpp:6:3: error: ‘Student’ was not declared in this scope
driver.cpp:6:11: error: expected ‘;’ before ‘student’
driver.cpp:8:3: error: ‘student’ was not declared in this scope
Student.cpp:7:1: error: ‘Student’ does not name a type
Student.cpp:9:1: error: ‘Student’ does not name a type
Student.cpp:16:1: error: ‘Student’ does not name a type
Student.cpp:18:6: error: ‘Student’ has not been declared
Student.cpp: In function ‘void display()’:
Student.cpp:20:21: error: ‘_id’ was not declared in this scope
Student.cpp:22:23: error: ‘_name’ was not declared in this scope
Jun 26 '11 #1
4 1998
weaknessforcats
9,208 Expert Mod 8TB
Because you say:

Expand|Select|Wrap|Line Numbers
  1. #include "Student.h"
in your driver.cpp it is required that student.h be in the same folder as driver.cpp.

If your student.h is in anther folder then you code:

Expand|Select|Wrap|Line Numbers
  1. #include <Student.h>
The angle brackets tell the preprocessor to locate your header along a pre-defined path. Consult the user's guide for your compiler as to how to d this.

Note that if you include using quotes and the included fle is not in the same fiolder as the file that includes it, the search for it reverts to the angle brackets usage.
Jun 27 '11 #2
whodgson
542 512MB
It would help if you put your code in code tags and reference the errors/warnings to line numbers.
Jun 27 '11 #3
In Student.h change
Expand|Select|Wrap|Line Numbers
  1. #ifdef STUDENT_H
to
Expand|Select|Wrap|Line Numbers
  1. #ifndef STUDENT_H
Jun 29 '11 #4
weaknessforcats
9,208 Expert Mod 8TB
Good show. I missed that. This is a reminder to actually try to reproduce the condition instead of just eyeballing it.
Jun 29 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alan Fried | last post by:
The following program is supposed to write the lower case letters of the alphabet to a text file. Here is the code: import java.io.*; class createFile4 { public static void main (String...
10
by: Cam | last post by:
Hi all, The code below is a practical exercise and works well running in the debug environment but fails when being compiled for a release build. I believe this is because the debug environment...
1
by: Hafeez | last post by:
I am having real trouble compiling this code http://www.cs.wisc.edu/~vganti/birchcode/codeHier/AttrProj.tgz The attachment shows errors when compiled using the current version of g++ in a...
6
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd...
0
by: Daniel | last post by:
Does system.xml have any way to transofrm data with an xswl style sheet using strings like MSXML2 does? how to convert this to use System.XML so i do not depend on MSXML2 interop? static...
1
by: klubbhead | last post by:
This part of my program needs to read from 4 parameter files and then displays them on the screen by clicking a different radio button. The program itself works, but my professor says that I have to...
1
by: vikas718 | last post by:
I need help to write a program using Visual c++ with winSDK or with WS Script Desktop lock application : 1. basically do desktop lock on activation (includes Task manager lock, start...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.