Basic C++ code issues/compiling

  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

Alright, first off I am just learning C++ from an eBook I found online. The problem is they are very vague on compiling things so I kind of "winged" it to check to see if my "exercise" code is correct.

Basically I have exer1.c:
Code: [ Select ]
char  LetterCode;
double SalePrice;
long  Quantity;

Lettercode = 'v';
SalePrice = 67.95;
Quantity  = 1000000;
  1. char  LetterCode;
  2. double SalePrice;
  3. long  Quantity;
  4. Lettercode = 'v';
  5. SalePrice = 67.95;
  6. Quantity  = 1000000;


I load up Konsole and type g++ exer1.c and I get these errors:
Quote:
exer1.c:5: ISO C++ forbids declaration of `Lettercode' with no type
exer1.c:6: ISO C++ forbids declaration of `SalePrice' with no type
exer1.c:6: conflicting types for `int SalePrice'
exer1.c:2: previous declaration as `double SalePrice'
exer1.c:6: warning: initialization to `int' from `double'
exer1.c:6: warning: argument to `int' from `double'
exer1.c:7: ISO C++ forbids declaration of `Quantity' with no type
exer1.c:7: conflicting types for `int Quantity'
exer1.c:3: previous declaration as `long int Quantity'
exer1.c:7:23: warning: no newline at end of file


So I try to debug the errors, by commenting out the first three lines and defining the variables with their assignment line like so:
Code: [ Select ]
//char  LetterCode;
//double SalePrice;
//long  Quantity;

char Lettercode = 'v';
double SalePrice = 67.95;
long Quantity  = 1000000;
  1. //char  LetterCode;
  2. //double SalePrice;
  3. //long  Quantity;
  4. char Lettercode = 'v';
  5. double SalePrice = 67.95;
  6. long Quantity  = 1000000;


And this comes up with 'g++ exer1.c':
Quote:
/usr/lib/gcc-lib/i486-slackware-linux/3.2.3/../../../crt1.o(.text+0x18): In function `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status


So I have no idea what that means, and I don't know why I have to declare the variable type with its value assignment, any ideas why this isn't working, or do I have it right and just can't compile without some sort of "real" script?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

Ok the problem came from my stupidity and lack of knowledge with the c++ object oriented programming, but I figured it out, I actually need to encapsule the whole code in main(){ } like:
Code: [ Select ]
main() {
char  LetterCode;
double SalePrice;
long  Quantity;

LetterCode = 'v';
SalePrice = 67.95;
Quantity  = 1000000L;
}
  1. main() {
  2. char  LetterCode;
  3. double SalePrice;
  4. long  Quantity;
  5. LetterCode = 'v';
  6. SalePrice = 67.95;
  7. Quantity  = 1000000L;
  8. }


I also had "Lettercode" instead of "LetterCode" before. :oops:
My real problem is I don't know if I have libraries installed anywhere on my box....
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9088
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Yup I was just about to tell you that you need to have at least a main() block. Looks like you figured it out though :)

You probably have many libraries installed on your computer, but it does depend on how you installed your computer. On my home Redhat server I usually install it with every development library/feature possible so that I will not have any problems using a common library if need be.
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

I downloaded the latest full version of gcc and now everything works.
  • Dragon
  • Proficient
  • Proficient
  • User avatar
  • Posts: 409
  • Loc: USA

Post 3+ Months Ago

Hey b_heyer would you please share that eBook with me pleaseeeeeeeeeeeeeeeeeeeeeeeeeeeeee....

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 66 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.