Odd error

This is a discussion on Odd error within the C++ Programming forums, part of the General Programming Boards category; I feel as if I have correctly setup my pointer and addresses within te arguments, but that's more than likely ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    30

    Odd error

    I feel as if I have correctly setup my pointer and addresses within te arguments, but that's more than likely where I'm going wrong.

    Quick question: Do you need the text object to be a pointer in the prototype of fadeMessage also?

    Code:
    #include <SFML/Graphics.hpp>
    #include <time.h>
    #include <string>
    
    
    using namespace std;
    
    
    void fadeMessage(sf::Text *text, unsigned int length);
    
    
    int main()
    {
        sf::RenderWindow window(sf::VideoMode(1000, 450), "Tpyor");
        window.setFramerateLimit(60);
    
    
        sf::Text text("Welcome to my game.", sf::Font::getDefaultFont(), 36);
        fadeMessage(sf::Text &text, 200);
    
    
        while(window.isOpen()){
            sf::Event event;
            while(window.pollEvent(event)){
                if(event.type == sf::Event::Closed)
                    window.close();
            }
    
    
            window.clear();
            window.draw(text);
            window.display();
        }
    
    
        return 0;
    }
    
    
    //text object, effect length (max 255)
    void fadeMessage(sf::Text *text, unsigned int length){
        *text.setString("worked");
    }
    Error:

    Code:
    C:\Users\Justin\Desktop\Logic\main.cpp||In function 'int main()':|
    C:\Users\Justin\Desktop\Logic\main.cpp|15|error: expected primary-expression before '&' token|
    C:\Users\Justin\Desktop\Logic\main.cpp||In function 'void fadeMessage(sf::Text*, unsigned int)':|
    C:\Users\Justin\Desktop\Logic\main.cpp|34|error: request for member 'setString' in 'text', which is of non-class type 'sf::Text*'|
    ||=== Build finished: 2 errors, 0 warnings ===|

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    fadeMessage(sf::Text &text, 200);

    &text would have been fine. This looks like a reference declaration, which is not possible in this context and confused the compiler.

    *text.setString("worked");

    Use the arrow operator:
    text->setString("worked"); equivalent to (*text).setString("worked");
    Yes, the parens matter.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    30
    Thank you! Simple mistakes on my part. ^.^

    Revised and working code:

    Code:
    #include <SFML/Graphics.hpp>
    #include <iostream>
    #include <string>
    
    
    
    
    using namespace std;
    
    
    
    
    void fadeMessage(sf::Text *text, int length);
    
    
    int main()
    {
        sf::RenderWindow window(sf::VideoMode(1000, 450), "Tpyor");
        window.setFramerateLimit(60);
    
    
    
    
        sf::Text text("Welcome to my game.", sf::Font::getDefaultFont(), 36);
        fadeMessage(&text, 200);
    
    
    
    
        while(window.isOpen()){
            sf::Event event;
            while(window.pollEvent(event)){
                if(event.type == sf::Event::Closed)
                    window.close();
            }
    
    
    
    
            window.clear();
            window.draw(text);
            window.display();
        }
    
    
    
    
        return 0;
    }
    
    
    
    
    //text object, effect length (max 255)
    void fadeMessage(sf::Text *text, int length){
        while(length > 0){
            length--;
            cout << "text->setColor(sf::Color(255,255,255,(" << length << ")));" << endl;
            text->setColor(sf::Color(255,255,255,length));
        }
    }
    Last edited by Justin H; 12-03-2012 at 08:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-29-2012, 03:33 AM
  2. Replies: 15
    Last Post: 11-28-2011, 10:48 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 1
    Last Post: 11-15-2010, 10:14 AM
  5. Replies: 3
    Last Post: 10-02-2007, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21