Thread: namespace question

  1. #1
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    Mario, this is about a 2002 thread...

    I'm currently compiling code on a linux box and running into namespace problems.
    As I go through the permutations of namespace combinations I'm able to mimic much of the action discussed here and in other threads.

    In the beginning all my classes were saying "...expected...before...token"
    Then I took out the namespace std and this helped because then I was able to see THAT my classes were suffering from the same types of problems.

    So, I was able to solve half the problems dealing with scope by playing with namespace, etc.
    BUT, the classes are still not working.
    The example below is a mix of std:: and not std:: to see what happens. I can add std:: to every call but what can make my classes work?

    Thanks 10^6
    Dave
    PS - I'm coding this summer...this never happened in C
    Will "namespaces" ultimately kill C++ ?

    It's all in progress...but I've clipped the larger code down to bare bones for testing.
    Thanks for the help in advance (begging by any other cleche)

    Code:
    cat -n Vtest.h (clipped)
         4  // The routines will initially return text messages for debugging and then later be overloaded for data
         5
         6  #include <iostream>
         7  #include <fstream>
         8  #include <stdio.h>
         9  #include <time.h>
        10  #include <math.h>
        11
        12  #include <usb.h>
        13  #include <libxxusb.h>
        14
        15  class vtest 
        16     {
        17     private:
        18      usb_dev_handle *udev;     // Device Handle 
        19      char scalerChar[20];
        20      long value;
        21     public:
        22      int icount;
        23      vtest(usb_dev_handle *udev);
        24      void vtestvoid(long &value);
        25      long vtestlong(long &value);
        26      char vtestchar(long &value);
        27      int vtestcount(int icount);
        28      int vtestvalue(void);
        29     };
        30
        31  vtest::vtest(usb_dev_handle *udev){
        32    icount = 0;
        33    value = 90:
        34  }
        35
        36  vtest::vtestvoid(&value){
        37    value = 98;
        38  }
        39
        40  vtest::vtestlong(&value){
        41    value = 99;
        42    return(value);
        43  }
    
    cat -n Vtest.cpp (clipped)
         5  #include <iostream>
         6  #include <fstream>
         7  #include "Vtest.h"   // case sensetive
         8
         9  std::cout cout;
        10  std::endl endl;
        11
        12  int CheckDevice(usb_dev_handle *udev){ //Check if device is open. If not, end program
        13      if(udev)
        14         {
        15         cout << "VM-USB device is open for communication" << endl;
        16         }
        17      else
        18         {
        19         cout << "No VM-USB devices found!" << endl;
        20         // fout << "No VM-USB devices found!" << endl; // send a message to the log file?
        21         return 0;   // won't end the program but useful to if()
        22         }
        23  }
        24
        25  int main ()
        26  {
        27      // initialize
        28      char logName[79];
        29      long value;
        30
        31      xxusb_device_type devices[99]; 
        32      struct usb_device *dev;
        33      usb_dev_handle *udev;       // Device Handle 
        34
        35      //Find XX_USB devices and open the first one found
        36      xxusb_devices_find(devices);
        37      dev = devices[0].usbdev;
        38      udev = xxusb_device_open(dev); 
        39      
        40      std::cout << "Trying to establish communication with the VM-USB" << std::endl;
        41   
        42      if (!CheckDevice(udev)) return 0;
     
    In file included from Vtest.cpp:7:
    Vtest.h: In constructor `vtest::vtest(usb_dev_handle*)':
    Vtest.h:33: error: expected `;' before ':' token
    Vtest.h: At global scope:
    Vtest.h:36: error: expected constructor, destructor, or type conversion before '(' token
    Vtest.h:36: error: expected `,' or `;' before '(' token
    Vtest.h:40: error: expected constructor, destructor, or type conversion before '(' token
    Vtest.h:40: error: expected `,' or `;' before '(' token
    Vtest.h:45: error: expected constructor, destructor, or type conversion before '(' token
    Vtest.h:45: error: expected `,' or `;' before '(' token
    Vtest.h:51: error: expected constructor, destructor, or type conversion before '(' token
    Vtest.h:51: error: expected `,' or `;' before '(' token
    Vtest.h:56: error: ISO C++ forbids declaration of `vtestvalue' with no type
    Vtest.cpp:9: error: expected constructor, destructor, or type conversion before "cout"
    Vtest.cpp:9: error: expected `,' or `;' before "cout"
    Vtest.cpp:10: error: expected constructor, destructor, or type conversion before "endl"
    Vtest.cpp:10: error: expected `,' or `;' before "endl"
    Vtest.cpp: In function `int CheckDevice(usb_dev_handle*)':
    Vtest.cpp:15: error: `cout' undeclared (first use this function)
    Vtest.cpp:15: error: (Each undeclared identifier is reported only once for each function it appears in.)
    Vtest.cpp:15: error: `endl' undeclared (first use this function)
    make: *** [Vtest] Error 1
    
    Makefile (clipped)
         3  .LIBPATTERNS    ="'lib&#37;.so lib%.a' "
         4
         5  VERBOSE         = 1
         6
         7  CXX             = g++ -v -g
         8
         9  RM              = rm *~
        10
        11  LD              = gcc-3.3
        12  LDFLAGS         = -O
        13  SOFLAGS         = -shared
        14
        15  CXXLIBDIRS      = -l /usr/include/lib/
        16  CXXLIBS         = -l xx_usb
        17  INCLUDEDIRS     = -I .
        18
        19  CXXFLAGS        = -O -Wall -fPIC -g $(INCLUDEDIRS)
        20
        21  LDFLAGS         = $(CXXFLAGS)
        22
        23  OBJ = Vtest.exe
        24
        25  Vtest: 
        26          $(CXX) $(CXXFLAGS) \
        27          $(CXXLIBS) $(CXXLIBDIRS) -o $(OBJ) Vtest.cpp
        28          @echo "Done"
    Last edited by Dave++; 06-01-2007 at 06:18 PM. Reason: "Use the code...Luke"

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.


    If you have any questions about this you may ask or you can contact one of our forum leaders:

    http://cboard.cprogramming.com/showgroups.php


    -------------------------------------------------

    Note that one of the rules here is to not bump old threads. Five years is pretty old. A better choice would be to create a new thread and link to one if you think it is pertinent.

    Your biggest problem doesn't appear to have anything to do with namespaces. On line 33 of vtest.h you have a colon : instead of a semicolon ;.

    In addition,
    Code:
    std::cout cout;
    is incorrect syntax. I assume you meant:
    Code:
    using std::cout;
    although I prefer to just use std::cout everywhere instead of the using directive or declaration.
    Last edited by Daved; 06-01-2007 at 07:27 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    Daved, Thanks for the response and feedback. I originally posted with out the "code" tags.

    Regretablly, the compiller persists in its error and after a week of trying to weed it out I did seem to boil it down to some type of workspace issue.

    > So, I was able to solve half the problems dealing with scope by playing with namespace, etc.
    > BUT, the classes are still not working.

    Could it be the compiler and the makefile?

    Code:
    In file included from Vtest.cpp:7:
    Vtest.h:36: error: expected constructor, destructor, or type conversion before '(' token
    Vtest.h:36: error: expected `,' or `;' before '(' token
    Vtest.h:40: error: expected constructor, destructor, or type conversion before '(' token
    Vtest.h:40: error: expected `,' or `;' before '(' token
    Vtest.h:45: error: expected constructor, destructor, or type conversion before '(' token
    Vtest.h:45: error: expected `,' or `;' before '(' token
    Vtest.h:51: error: expected constructor, destructor, or type conversion before '(' token
    Vtest.h:51: error: expected `,' or `;' before '(' token
    Vtest.h:56: error: ISO C++ forbids declaration of `vtestvalue' with no type
    I know its an easy one...and after googling for days I've seen others say "solved" but no one actually said "how..."

    Regards,
    Dave
    Last edited by Dave++; 06-03-2007 at 08:45 PM. Reason: /\\/\/\

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Shot in the dark:
    Code:
    vtest::vtest(usb_dev_handle *udev){
      icount = 0;
      value = 90:
    }
    
    vtest::vtestvoid(&value){
      value = 98;
    }
    
    vtest::vtestlong(&value){
      value = 99;
      return(value);
    }
    Perhaps the last two are missing a type, not to mention a return type?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    Thanks Dave,
    Yes, I realized that my format in the header file was screwed up late last night.
    In the old days I'd just search for zeros and ohs or lines longer that the card could take.

    Code:
    // example: class constructor
    #include <iostream>
    using namespace std;
    
    class CRectangle {
        int width, height;
      public:
        CRectangle (int,int);
        int area () {return (width*height);}
    };
    
    CRectangle::CRectangle (int a, int b) {
      width = a;
      height = b;
    }
    
    int main () {
      CRectangle rect (3,4);
      CRectangle rectb (5,6);
      cout << "rect area: " << rect.area() << endl;
      cout << "rectb area: " << rectb.area() << endl;
      return 0;
    }
    Thanks for the help. This is one of those 2x4 problems.

    Dave

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Dave++ View Post
    Code:
    CRectangle::CRectangle (int a, int b) {
      width = a;
      height = b;
    }
    FWIW, consider using initialization lists
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. A fourth noobie question - namespace std?
    By Noobie in forum C++ Programming
    Replies: 24
    Last Post: 08-12-2005, 02:10 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. using namespace std; question.
    By fuh in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2002, 04:39 PM